Skip to content

Instantly share code, notes, and snippets.

@brookslyrette
Created January 28, 2016 21:12
Show Gist options
  • Save brookslyrette/568da0f230764c1ad352 to your computer and use it in GitHub Desktop.
Save brookslyrette/568da0f230764c1ad352 to your computer and use it in GitHub Desktop.
java backup and restore of h2 for unit tests
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = {SampleApplication.class}, name = "acceptanceTestContext")
@WebIntegrationTest
public abstract class AbstractAcceptanceTests {
@Autowired
protected JdbcTemplate jdbcTemplate;
/**
* Backs up the database before running this test.
*/
@Before
public void setUpDatabase() {
//backup existing database
jdbcTemplate.execute("SCRIPT TO './build/tmp/h2-backup.sql'");
}
/**
* Restores the database from the backup file.
*/
@After
public void restoreDatabase() {
//restore backup from file.
jdbcTemplate.execute("DROP ALL OBJECTS");
jdbcTemplate.execute("RUNSCRIPT FROM './build/tmp/h2-backup.sql'");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment