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());
}
}
我用过
javac -cp /root/Documents/unit\ tests/junit-4.10.jar TestJunit.java TestRunner.java
编译并
java -cp /root/Documents/unit\ tests/junit-4.10.jar:. org.junit.runner.JUnitCore TestRunner
运行。
它编译时没有错误,但测试失败并出现异常。我的测试方法用 @Test
注释。为什么我会收到此错误?
最佳答案
当您在命令行参数中传递的类没有任何用 @Test
注释的方法时,您会遇到该异常。在本例中,TestRunner
没有任何用 @Test
注解的方法。
要运行单元测试,请执行以下操作:
java -cp /root/Documents/unit\ tests/junit-4.10.jar:. org.junit.runner.JUnitCore TestJunit
或者:
java -cp /root/Documents/unit\ tests/junit-4.10.jar:. TestRunner
关于java - 从命令行进行 Junit 测试 无可运行方法异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36244925/