Andrey Agibalov
Andrey Agibalov

Reputation: 7694

JUnit test inheritance doesn't work

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.

Upvotes: 11

Views: 7352

Answers (2)

Andrey Agibalov
Andrey Agibalov

Reputation: 7694

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".

Upvotes: 15

Sam Goldberg
Sam Goldberg

Reputation: 6801

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?

Upvotes: 0

Related Questions