Spring, spring batch, hibernate, and JUnit and initialization for multiple integration tests
I have some test classes that are making use of spring-batch, and hibernate. My test contexts establish an h2 in memory database, initialized the hibernate session factory, which creates most of the db schema, and also executes some specific SQL scripts to set up the database schema objects required for spring batch (non-orm). Hibernate does a very nice job only doing what is necessary, however the spring-batch initialization scripts are not as smart.
My test configuration uses an h2 embedded database, so I can safely initialize everything once, however, I need to convey开发者_JS百科 to spring to only initialize the rest integration infrastructure (e.g hbm session factory, and spring-batch schemas) once for the whole collection of tests being run.
My unit tests all run individually, but tests 2 through N fail when testing the whole package as they are performing the same db schema initialization repeatedly, and dying on (db) object already exists errors.
I am looking to initialize an application context for the whole collection of integration tests, and then delegate configuration of the details for a specific test to the MyTest-context, yet still initialize the main application context when running a single integration test.
I have a common set of spring configuration files which are imported into test context to initialize the infrastructure needed to tests application services. The problem comes from running all tests in a package, etc. The test system calls the same initialization multiple times.
I am hoping this is a pretty common problem and that I overlooked a detail in spring or JUnit's documentation.
- By default, Spring caches ApplicationContext instances that are loaded for tests. The only reasons that it would init a new one are that a test uses a different set of context files and hence needs a different context or a test makes use of the @DirtiesContext annotation.
- Why not just add an "if not exists" to your sql scripts?
精彩评论