Issue
I have a test class that uses @TestPropertySource
to load a hsqldb
.
Essentially, the test file has gotten quite large, and I would like to break it up a bit. The problem is that loading the DB takes... some time. Not too much, but I don't want to create several test files that each load up the DB.
My code looks like this:
@TestPropertySource(properties = {
"hsqldb.name=SettingsTest"
})
@ContextConfiguration(classes = { settings.config.Config.class }, loader = AnnotationConfigContextLoader.class)
@DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_CLASS)
public class SvcTest
.
.
.
This will run some functions to load up the database. It is my understanding that once the test file is done with all its tests, it will stop the DB. How can I keep it running so other files can use the DB, and close it only when they are done?
Solution
Attempting to cache the application context between test classes while at the same time marking the tests as "dirty" using @DirtiesContext
is a bit contradictory:
DirtiesContext: Test annotation which indicates that the ApplicationContext associated with a test is dirty and should therefore be closed and removed from the context cache.
If you use the annotation solely to reset the state of your database, you could instead create a ClassRule
to manually reset the test data in the database instead of tearing down and rebuilding the full application context.
Furthermore, if the tests are focused on the repository section of the application, Spring offers test slicing
. Test slices load a small fragment of the application context, thereby reducing the load time. An example is the @JdbcTest
annotation, used for JDBC tests that focus on JDBC-based components.
Answered By - Michiel