TestJunit class
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class TestJunit
{
@Test
public void testAdd()
{
String str= "Junit is working fine";
assertEquals("Junit is working fine",str);
}
}
TestRunner class
/********************************************/
import org.junit.runner.JUnitCore;
import org.junit.runner.Result;
import org.junit.runner.notification.Failure;
public class TestRunner {
public static void main(String[] args) {
Result result = JUnitCore.runClasses(TestJunit.class);
for (Failure failure : result.getFailures()) {
System.out.println(failure.toString());
}
System.out.println(result.wasSuccessful());
}
}
I used
javac -cp /root/Documents/unit\ tests/junit-4.10.jar TestJunit.java TestRunner.java
to compile and
java -cp /root/Documents/unit\ tests/junit-4.10.jar:. org.junit.runner.JUnitCore TestRunner
to run. It compiles without errors but the tests fail with an exception. My test methods are annotated with @Test
. Why do I get this error?
You get that exception when the class that you passed in the command line arguments doesn't have any methods annotated with @Test
. In this case, TestRunner
doesn't have any methods annotated with @Test
.
To run your unit tests, do:
java -cp /root/Documents/unit\ tests/junit-4.10.jar:. org.junit.runner.JUnitCore TestJunit
or alternatively:
java -cp /root/Documents/unit\ tests/junit-4.10.jar:. TestRunner
In my case the test class worked fine from IDE but it didn't work from a commandline.
I only got it to work when I added a "Test" postfix to the test class name.
I was using JUnit 4.12.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.