111

I am trying to find an approach that will allow me to run a single test from a JUnit class using only command-line and java.

I can run the whole set of tests from the class using the following:

java -cp .... org.junit.runner.JUnitCore org.package.classname

What I really want to do is something like this:

java -cp .... org.junit.runner.JUnitCore org.package.classname.method

or:

java -cp .... org.junit.runner.JUnitCore org.package.classname#method

I noticed that there might be ways to do this using JUnit annotations, but I would prefer to not modify the source of my test classes by hand (attempting to automate this). I did also see that Maven might have a way to do this, but if possible I would like to avoid depending on Maven.

So I am wondering if there is any way to do this?


Key points I'm looking for:

  • Ability to run a single test from a JUnit test class
  • Command Line (using JUnit)
  • Avoid modifying the test source
  • Avoid using additional tools
Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
Kevin Jalbert
  • 3,115
  • 3
  • 26
  • 39
  • 5
    Cool, another "Unix is my IDE" type enthusiast. I like using an IDE for an editor (particularly with the vim plugin for NetBeans), but I agree that small sharp tools that last "forever", and can be assembled in arbitrary configurations, are often better than constantly relearning esoteric features of a continuing series of IDE brands and versions. Good for you! – Roboprog Apr 25 '12 at 17:03
  • 2
    You said you wanted to avoid maven. For anyone who wants to do this using maven see look here, http://maven.apache.org/plugins/maven-surefire-plugin/examples/single-test.html – reevesy Jul 18 '12 at 09:36

4 Answers4

85

You can make a custom, barebones JUnit runner fairly easily. Here's one that will run a single test method in the form com.package.TestClass#methodName:

import org.junit.runner.JUnitCore;
import org.junit.runner.Request;
import org.junit.runner.Result;

public class SingleJUnitTestRunner {
    public static void main(String... args) throws ClassNotFoundException {
        String[] classAndMethod = args[0].split("#");
        Request request = Request.method(Class.forName(classAndMethod[0]),
                classAndMethod[1]);

        Result result = new JUnitCore().run(request);
        System.exit(result.wasSuccessful() ? 0 : 1);
    }
}

You can invoke it like this:

> java -cp path/to/testclasses:path/to/junit-4.8.2.jar SingleJUnitTestRunner 
    com.mycompany.product.MyTest#testB

After a quick look in the JUnit source I came to the same conclusion as you that JUnit does not support this natively. This has never been a problem for me since IDEs all have custom JUnit integrations that allow you to run the test method under the cursor, among other actions. I have never run JUnit tests from the command line directly; I have always let either the IDE or build tool (Ant, Maven) take care of it. Especially since the default CLI entry point (JUnitCore) doesn't produce any result output other than a non-zero exit code on test failure(s).

NOTE: for JUnit version >= 4.9 you need hamcrest library in classpath

user1516873
  • 5,060
  • 2
  • 37
  • 56
Mark Peters
  • 80,126
  • 17
  • 159
  • 190
  • 2
    Actually, I seem to remember getting a full stack trace in the log when an assertion in a test failed, as well as the (optional, when defined) message from each assertion describing the unrealized expectation. Thanks for the work-around. – Roboprog Apr 25 '12 at 17:01
64

I use Maven to build my project, and use SureFire maven plugin to run junit tests. Provided you have this setup, then you could do:

mvn -Dtest=GreatTestClass#testMethod test

In this example, we just run a test method named "testMethod" within Class "GreatTestClass".

For more details, check out http://maven.apache.org/surefire/maven-surefire-plugin/examples/single-test.html

Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
Yiling
  • 2,817
  • 2
  • 21
  • 27
0

We used IntelliJ, and spent quite a bit of time trying to figure it out too.

Basically, it involves 2 steps:

Step 1: Compile the Test Class

% javac -cp .:"/Applications/IntelliJ IDEA 13 CE.app/Contents/lib/*" SetTest.java

Step 2: Run the Test

% java -cp .:"/Applications/IntelliJ IDEA 13 CE.app/Contents/lib/*" org.junit.runner.JUnitCore SetTest

Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
Benjamin Tan Wei Hao
  • 9,621
  • 3
  • 30
  • 56
0

The following command works fine.

mvn -Dtest=SqsConsumerTest -DfailIfNoTests=false test
kanaparthikiran
  • 523
  • 12
  • 15