[英]Junit test from commandline No runnable method exception
TestJunit class TestJunit类
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
. 我的测试方法带有@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
. 当您在命令行参数中传递的类没有任何带@Test
注释的方法时,您将获得该异常。 In this case, TestRunner
doesn't have any methods annotated with @Test
. 在这种情况下, TestRunner
没有使用@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. 就我而言,测试类在IDE中可以正常工作,但在命令行中却无法工作。
I only got it to work when I added a "Test" postfix to the test class name. 只有在测试类名称中添加“测试”后缀时,它才能正常工作。
I was using JUnit 4.12. 我正在使用JUnit 4.12。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.