【问题标题】:Junit test from commandline No runnable method exception来自命令行的 Junit 测试没有可运行的方法异常
【发布时间】:2016-03-27 07:30:41
【问题描述】:

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 注释。为什么会出现此错误?

【问题讨论】:

  • 运行的时候一定要提到TestJunit,而不是TestRunner。

标签: java unit-testing exception junit


【解决方案1】:

当您在命令行参数中传递的类没有任何用@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

【讨论】:

    【解决方案2】:

    在我的情况下,测试类在 IDE 中运行良好,但在命令行中无法运行。

    当我在测试类名称中添加“测试”后缀时,我才让它工作。

    我使用的是 JUnit 4.12。

    【讨论】:

      猜你喜欢
      • 2014-08-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-30
      • 1970-01-01
      • 2016-11-03
      • 2012-06-24
      • 1970-01-01
      相关资源
      最近更新 更多