You should be able to use Spring's embedded database configuration to specify an H2 datasource (also shown here):
<jdbc:embedded-database id="dataSource" type="H2">
<!-- Modify locations appropriately for your environment -->
<jdbc:script location="classpath:db-schema.sql"/>
<jdbc:script location="classpath:db-test-data.sql"/>
</jdbc:embedded-database>
That should go in your test-specific testApplicationContext.xml
with the appropriate namespace declarations.
Spring will create the H2 datasource when it brings up the test application context at the beginning of the test suite (test class). Because Spring caches the application context for the duration of the test class, you can annotate the test class with @DirtiesContext
so that the application context is re-created and datasource re-initialized for each test method:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({"classpath:/your/testApplicationContext.xml"})
@DirtiesContext(classMode=ClassMode.AFTER_EACH_TEST_METHOD)
public class SomeDatabaseTest {
@Autowired
private SomeDao dao;
// Test methods
}
More info on Spring's embedded database functionality can be found here