Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ammachado/c564e2677a950b55b155eba4a8e85db6 to your computer and use it in GitHub Desktop.
Save ammachado/c564e2677a950b55b155eba4a8e85db6 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