JUnit test inheritance doesn't work
Asked Answered
G

2

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.

Greave answered 10/1, 2012 at 19:18 Comment(9)
Do you need to extend TestCase?Dram
Is it required even in case I use annotations?Greave
Junit4 doesn't require you to extend TestCase but how are you running these? From the command line? From Eclipse?Metamorphose
@jeff: mvn clean test. That works fine for other tests in the same package, the only difference is, these tests doesn't use inheritance.Greave
Can you include the content of one of the subclassesKay
What is B? surely it should be Number unless you sub-classed a class with 2 classes with the same name, Double and Integer, as a classes in the core library?Kay
@whatsthebeef: it DOESN'T matter :-)Greave
I would have thought with your rep you have been exposed to the level of pedantry required on this site, obviously not.Kay
@whatsthebee: Sorry, I see what you mean now. Yes, I should have used Number instead of B. It's pretty minor detail, but yes, it should be more pedantic. I'll fix it.Greave
G
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".

Greave answered 10/1, 2012 at 19:44 Comment(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... maven.apache.org/plugins/maven-surefire-plugin/examples/…Pelayo
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: maven.apache.org/plugins/maven-surefire-plugin/…Laryssa
A
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?

Amii answered 10/1, 2012 at 19:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.