I have a Spring/JPA web application that I would like to write some tests for. Ideally I would like to be able to:
- create the test DB schema (from the JPA annotated classes) once before the tests are run
- run each test method in it's own transaction which is rolled back when the test completes
- specify the (DbUnit) dataset to be loaded for each test either at the per-class or per-method level. The test data should be loaded after the transaction has started so that the test data will also be rolled back when the test completes
- inject Spring beans into the test class
I'm aware that Spring provides classes which can provide the transactional behaviour I'm seeking. Ideally, the final solution will look something like this
// This dataset will be used for all tests that don't override it with their own annotation
@TestData('/dbunit/dataSetDefault.xml')
public class MyTests extends ProbablySomethingFromTheSpringFramework {
@Test
void testWithDefaultDataSet() {
// Transaction is implicitly started here
// My test code goes here
// A transaction is implicitly rolled-back here
}
@TestData('/dbunit/dataSetCustom.xml')
@Test
void testWithCustomDataSet() {
// Same as the other test
}
}
Obviously the parent class and the @TestData
are fictitious, is there something available that provides the functionality I'm looking for?
This leaves the question of how to create the test DB schema. Ideally, this would happen once before all the tests are run (by Maven). Can someone suggest how I might achieve this? I imagine it involves using something to convert the JPA annotations to DDL, then something else to load that into the test database schema.
Thanks!