Search code examples
javamavenjunitmaven-surefire-plugin

JUnit Category doesn't work with TestCase?


I found a strange thing and I'm interested to know why it happens. I'm using maven surefire plugin ( 2.12.4 ) with Junit 4.11. When I wanted to use @Category annotation in order to disable some tests. the strange thing that it works correctly only with tests that don't extend TestCase. For test that don't extend TestCase I was able to put the annotation only on the test method to run/disable, but with others it disables all tests in the class. Example: Command Line: mvn test -Dgroups=!testgroups.DisabledTests run only test B for the first snippet:

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

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

for the second case with class extending TestCase, it will run no tests.

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

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

Why it happens?


Solution

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