How to listen to Test Suite changes in order to change some configurations
-
11-06-2021 - |
Pregunta
I have many Spring Test classes (defined using @RunWith(SpringJUnit4ClassRunner)
) that I want to group in test suites so that I load new configurations once for every suite instead of for every test class.
Right now I have a TestExecutionListener
listener to every Spring test that will reload the configurations, but I want to save that since many tests share the same configurations.
Sadly, niether Spring supports listening to JUnit TestSuite
s nor JUnit
supports the listener concept for TestSuite
s.
Solución
If you wish to perform actions before/after a junit test suite, you can use @BeforeClass and @AfterClass, or even @ClassRule. You can do your setup in there for the entire suite. Using the following example:
@RunWith(Suite.class)
@SuiteClasses({ ExpectedFailureTest.class, RetryTest.class })
public class BeforeClassSuite {
@BeforeClass
public static void beforeClass() {
System.out.println("beforeClass");
}
@AfterClass
public static void afterClass() {
System.out.println("afterClass");
}
@ClassRule
public static ExternalResource resource = new ExternalResource() {
@Override
protected void before() throws Throwable {
System.out.println("before");
};
@Override
protected void after() {
System.out.println("after");
};
};
}
This produces as output:
before
beforeClass
... output from tests
afterClass
after