JUnit类别不适用于TestCase?

时间:2016-01-06 14:28:22

标签: java maven junit maven-surefire-plugin

我发现了一件奇怪的事情,我很想知道它为什么会发生。我正在使用maven surefire插件(2.12.4)和Junit 4.11。当我想使用@Category注释来禁用某些测试时。奇怪的是它只能在不扩展TestCase的测试中正常工作。对于不扩展TestCase的测试,我只能将注释放在测试方法上运行/禁用,但是对于其他人,它会禁用类中的所有测试。 例: 命令行: mvn test -Dgroups=!testgroups.DisabledTests仅针对第一个代码段运行测试B:

import static org.junit.Assert.*;
public class DataTest {
    @Test
    public void testA(){...} 

    @Test @Category(testgroups.DisabledTests.class)
    public void testB(){...}
}

对于扩展TestCase的类的第二种情况,它将不运行任何测试。

public class DataTest extends TestCase {
    @Test
    public void testA(){...} 

    @Test @Category(testgroups.DisabledTests.class)
    public void testB(){...}
}

为什么会这样?

1 个答案:

答案 0 :(得分:0)

The solution was given in a comment by deborah-digges:

The problem is that the second class is an extension of TestCase. Since this is JUnit 3 style, the annotation @Category didn't work. Annotating the class with @RunWith(JUnit4.class) should give you the same result in both cases

and another by Stefan Birkner:

JUnit 4 finds test by looking for the @Test annotation. You can remove the extends TestCase from your test class. Furthermore the name of the test method does no longer have to start with test. You're free to choose any method name you want.