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> { ... }

根本不执行任何测试,两个具体类都被忽略。我如何使它工作?(我希望test1()同时执行IntegerDouble)。

我使用 JUnit 4.8.1。

更新:似乎问题与 maven-surefire-plugin 相关,而不是 JUnit 本身。请看下面我的回答。

4

2 回答 2

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

于 2012-01-10T19:44:43.433 回答
0

我使用您的框架代码在 Eclipse 中对此进行了测试,并且效果很好:

基类:

package stkoverflow;

import org.junit.Test;

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

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

子类:

package stkoverflow;

public class ConcreteTests1 extends GenericTests<Integer> {

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

在 Eclipse Junit Runner 中运行 ConcreteTests1 运行良好。也许问题出在Maven?

于 2012-01-10T19:47:00.670 回答