java - 从命令行进行 Junit 测试 无可运行方法异常

标签 java unit-testing exception junit

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/

相关文章:

java - Netbeans 编辑器库?

java - 单元测试双向转换器

c# - 来自无法访问的代码的无法捕获的异常

c# - nunit resharper 预期异常测试

java - Postgres 异常和 java

java - 更改 YourKit Remote Profiler 临时目录

java - Hibernate 连接到错误的数据库

java - Android - 单元测试无法引用 Android 类

java - 当框架中有两个 JPanel 时,在一个 JPanel 上绘画

c# - AutoFixture+Moq - 卡住模拟类以防止设置