使用`mvn test`参数化JUnit测试是否正确?

我刚刚使用JUnit 4.11实现了一个JUnit测试用例:

https://github.com/junit-team/junit/blob/master/doc/ReleaseNotes4.11.md#example-1

我使用创建一个maven项目

  4.0.0 com.yyt example 0.0.1-SNAPSHOT   junit junit 4.11 test    

而这个测试用例:

 import java.util.Arrays; import org.junit.Assert; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.Parameterized; import org.junit.runners.Parameterized.Parameters; @RunWith(Parameterized.class) public class Example { @Parameters(name = "{index}: fib({0})={1}") public static Iterable data() { return Arrays.asList(new Object[][] { { 0, 0 }, { 1, 1 }, { 2, 1 }, { 3, 2 }, { 4, 3 }, { 5, 5 }, { 6, 8 } }); } private int input; private int expected; public Example(int input, int expected) { this.input = input; this.expected = expected; } @Test public void test() { } } 

但是,当我使用mvn test ,maven说:

 ------------------------------------------------------- TESTS ------------------------------------------------------- There are no tests to run. 

如何使其工作?

问题是maven的命名约定,它基于maven-surefire-plugin ,需要命名如Test.javaTest .javaTestCase * .java进行unit testing。 对于集成测试, maven-failsafe-plugin负责具有命名约定IT * .java* IT.java* ITCase.java

@khmarbaise在这种情况下钉了它,但……

有时你冒险在这里因为

  • Maven和Eclipse都告诉你,你class上没有考试
  • 显然做的; 而且名字很好
  • 这两个工具似乎找到了正确的类并尝试在那里执行测试
  • 你开始怀疑Maven和Parameterized是否一起工作,这个问题的标题听起来很有希望

如果满足上面列出的相关条件,您的Maven输出应该沿着这些行说明:

 ------------------------------------------------------- TESTS ------------------------------------------------------- Running your.maven.properly.named.SomeTest Tests run: 0, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 7.684 sec Results : Tests run: 0, Failures: 0, Errors: 0, Skipped: 0 [INFO] ------------------------------------------------------------------------ [ERROR] BUILD FAILURE [INFO] ------------------------------------------------------------------------ [INFO] No tests were executed! (Set -DfailIfNoTests=false to ignore this error.) 

如果确实如此,我的建议是:尝试查看您用于获取数据的任何内容实际上是否返回任何内容 。 换句话说:看看用@Parameters注释的方法。 没有数据,您的测试将无法运行。

陷入了同样的问题。 在junit https://github.com/junit-team/junit4/issues/664中找到了这个(仍然)未解决的问题。 所以,现在没办法让它发挥作用。