【问题标题】:JUnit test inheritance doesn't workJUnit 测试继承不起作用
【发布时间】:2012-02-07 05:06:40
【问题描述】:
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 本身。请参阅下面的答案。

【问题讨论】:

  • 需要扩展TestCase吗?
  • Junit4 不需要你扩展 TestCase 但你是如何运行这些的?从命令行?来自 Eclipse?
  • 能否包含其中一个子类的内容
  • 什么是 B?当然它应该是 Number 除非你用 2 个同名的类,Double 和 Integer 对一个类进行子类化,作为核心库中的一个类?
  • 我本以为你的代表已经接触到了这个网站所要求的迂腐水平,显然不是。

标签: java maven junit surefire


【解决方案1】:

将我所有的类重命名为后缀“Test”,现在它可以工作了(Concrete1TestConcrete2Test)。

更新

这与 maven-surefire-plugin 的默认设置有关。

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

默认情况下,Surefire 插件会自动包含所有具有以下通配符模式的测试类:

**/Test*.java - 包括其所有子目录和所有以“Test”开头的 java 文件名。 **/*Test.java - 包括其所有子目录和所有以“Test”结尾的 java 文件名。 **/*TestCase.java - 包括其所有子目录和所有以“TestCase”结尾的 java 文件名。

【讨论】:

【解决方案2】:

我在 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 上?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-03
    相关资源
    最近更新 更多