I have @BeforeClass setup that I run run for a test suite, like so:
@RunWith(Categories.class)
@IncludeCategory(IntegrationTest.class)
.
.
.
public class IntegrationTestSuite {
@BeforeClass
public static void initialise() throws Exception {
// Integration test-specific config.
}
}
This works well when I have all my tests running through the suite. However, when I run individual tests, obviously this stuff won’t be executed. Is there a more elegant way that would allow me to reuse the test category setup at a test case level?
Consider creating a custom rule (possibly utilizing ExternalResourse) that will execute the initialization only once. Using a mechanism where one test does initialization for other tests is an anti-pattern. It is too brittle because it depends on the order in which the tests are run and also fails when only a single test is run. I think the
@Rulemechanism is a much better solution.