Thursday, 12 September 2013

Do JUnit test suites support custom annotations?

Do JUnit test suites support custom annotations?

In JUnit, you can create test suites like so:
public class SecurityTest1 {
@Test
public void testSecurity1() {
// ...
}
}
public class LoadTest1 {
@Test
public void testLoad1() {
// ...
}
}
public class SecurityTest2 {
@Test
public void testSecurity2() {
// ...
}
}
@RunWith(Suite.class)
@SuiteClasses({SecurityTest1.class, SecurityTest2.class})
public class SecurityTestSuite {}
But this seems rather cumbersome. It would be so nice to define a simple
class-level annotation:
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface SecurityTest {}
And then define your suite like so:
@SecurityTest
public class SecurityTest1 {
@Test
public void testSecurity1() {
// ...
}
}
public class LoadTest1 {
@Test
public void testLoad1() {
// ...
}
}
@SecurityTest
public class SecurityTest2 {
@Test
public void testSecurity2() {
// ...
}
}
@RunWith(Suite.class)
@SuiteClasses({SecurityTest.class})
public class SecurityTestSuite {}
Is this possible? If so how? Note: not interested in switching to TestNG
or any other test framework if JUnit does not support this...thanks in
advance!

No comments:

Post a Comment