4

I'm just implemented a JUnit test case using JUnit 4.11 following the example:

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

I create a maven project using

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

    <modelVersion>4.0.0</modelVersion>
    <groupId>com.yyt</groupId>
    <artifactId>example</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>         
    </dependencies>

</project>

And this test case:

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<Object[]> 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() {
      }
}

But when I test it using mvn test , maven said:

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
There are no tests to run.

How to make it work?

jackalope
  • 1,554
  • 3
  • 17
  • 37

4 Answers4

10

The problem is the naming convention of maven which is based on maven-surefire-plugin which needs a naming like Test.java, Test.java or TestCase*.java for unit tests. For integration tests the maven-failsafe-plugin is responsible which has the naming convention IT*.java, *IT.java or *ITCase.java.

khmarbaise
  • 92,914
  • 28
  • 189
  • 235
  • Currently, this behavior is documented at http://maven.apache.org/surefire/maven-surefire-plugin/examples/inclusion-exclusion.html – user7610 Dec 02 '17 at 10:47
2

Got stuck on the same problem. Found this (still) open issue in junit https://github.com/junit-team/junit4/issues/664. So, there is no way to make it work now.

artkoshelev
  • 872
  • 7
  • 22
1

@khmarbaise nailed it in this case, but...

On occasion you ventured here because

  • Maven and Eclipse both tell you you have no tests in your class
  • you clearly do; and well-named
  • both tools seem to find the right class and try to execute tests there
  • you're starting to doubt if Maven and Parameterized work at all together and the title of this question sounds promising

Your Maven output should state something along these lines if related conditions listed above are met:

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
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.)

if it does, my suggestion is: try looking if whatever you use to get data actually returns anything. In other words: take a look at method annotated with @Parameters. Your tests won't run without data.

0

the only solution i know - is to run a whole class, not a test in class.

ex that works:

mvn clean -Dtest=ExampleClassTest test

ex that doesnt work:

mvn clean -Dtest=ExampleClassTest#test test

but be careful, all tests in the class will run with the parameters!

dsd
  • 1
  • 2