繁体   English   中英

从命令行进行Junit测试没有可运行的方法异常

[英]Junit test from commandline No runnable method exception

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

就我而言,测试类在IDE中可以正常工作,但在命令行中却无法工作。

只有在测试类名称中添加“测试”后缀时,它才能正常工作。

我正在使用JUnit 4.12。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM