我发现了一件奇怪的事情,我很想知道它为什么会发生。我正在使用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(){...}
}
为什么会这样?
答案 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 theextends TestCase
from your test class. Furthermore the name of the test method does no longer have to start withtest
. You're free to choose any method name you want.