Spring Test helpfully rolls back any changes made to the database within a test method. This means that it is not necessary to take the time to delete/reload the test data before each test method.
But if you use the @BeforeClass Junit annotation, then that forces the data loader to be static. A question that is explored here: Why must jUnit's fixtureSetup be static?
If the data initialization method is static, so must the data connection methods and the data source..and on and on…forcing everything to be static…which won’t work. At which point, I ask – what good is Spring Test’s ability to rollback changes when you have to delete/reload the test data anyway for every test??!?!
One approach that works is to create a “data initialiser” class, add it to a test Spring application context that also has your data source, and wire this application context into your tests. This relies on the fact that Spring caches the application context between test invocations.
For example, a test superclass:
With
test-application-context.xml:And
In this example:
DataLoadingTest;DatabaseInitialiser.load(), via the@PostConstructannotation;DatabaseInitialiserfrom the application context, which is already cached;Likewise,
DatabaseInitialisercan have a method annotated@PostDestroyto perform any rollback necessary at the end of the whole test run.