11
public abstract class GenericTests<T extends Number> {
  protected abstract T getT();      

  @Test public void test1() {
    getT();
  }
}

public class ConcreteTests1 extends GenericTests<Integer> { ... }
public class ConcreteTests2 extends GenericTests<Double> { ... }

No tests are executed at all, both concrete classes are ignored. How do I make it work? (I expect test1() to be executed for both Integer and Double).

I use JUnit 4.8.1.

Update: it appeared that problem is related with maven-surefire-plugin and not JUnit itself. See my answer below.

Andrey Agibalov
  • 7,624
  • 8
  • 66
  • 111

2 Answers2

15

Renamed all my classes to have suffix "Test" and now it works (Concrete1Test, Concrete2Test).

Update:

That's related with default settings of maven-surefire-plugin.

http://maven.apache.org/plugins/maven-surefire-plugin/examples/inclusion-exclusion.html

By default, the Surefire Plugin will automatically include all test classes with the following wildcard patterns:

**/Test*.java - includes all of its subdirectories and all java filenames that start with "Test". **/*Test.java - includes all of its subdirectories and all java filenames that end with "Test". **/*TestCase.java - includes all of its subdirectories and all java filenames that end with "TestCase".

Community
  • 1
  • 1
Andrey Agibalov
  • 7,624
  • 8
  • 66
  • 111
  • 2
    This is because the surefire-plugin maven uses to run tests "discovers" tests based on class names. They have to match one of a few accepted patterns, BlahTest being one of them. Here is more information on the default settings and how you can customize them in your config... http://maven.apache.org/plugins/maven-surefire-plugin/examples/inclusion-exclusion.html – Jesse Webb Jan 10 '12 at 19:51
  • 1
    Yep, this makes sense. The default file names used by the surefire plugin (the maven plugin that executes when you call `mvn test`) are `**/Test*.java`, `**/*Test.java` and `**/*TestCase.java`. Ref: http://maven.apache.org/plugins/maven-surefire-plugin/test-mojo.html#includes – matsev Jan 10 '12 at 19:51
0

I tested this in Eclipse, using your skeleton code, and it worked fine:

Base Class:

package stkoverflow;

import org.junit.Test;

public abstract class GenericTests<T> {
    protected abstract T getT();

    @Test
    public void test1() {
        getT();
    }    
}

Subclass:

package stkoverflow;

public class ConcreteTests1 extends GenericTests<Integer> {

    @Override
    protected Integer getT() {
        return null;
    }    
}

Running ConcreteTests1 in Eclipse Junit Runner worked fine. Perhaps the issue is with Maven?

Sam Goldberg
  • 6,711
  • 8
  • 52
  • 85