Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- jdbc:derby:memory:mydb;create=true
- jdbc:derby:memory:mydb;drop=true
- @RunWith(SpringJUnit4ClassRunner.class)
- @ContextConfiguration({"classpath*:application-context-test.xml"})
- @TestExecutionListeners({DirtiesContextTestExecutionListener.class,
- DependencyInjectionTestExecutionListener.class})
- @DirtiesContext(classMode = ClassMode.AFTER_EACH_TEST_METHOD)
- public abstract class AbstractTest {
- }
- hibernate.hbm2ddl.auto = create-drop
- mydb.drop.url = jdbc:derby:memory:mydb;drop=true
- ...
- <bean id="mydbDropUrl" class="java.lang.String">
- <constructor-arg value="${mydb.drop.url}" />
- </bean>
- ...
- @Resource
- private String mydbDropUrl;
- @After
- public void tearDown() {
- try {
- DriverManager.getConnection(mydbDropUrl);
- } catch (SQLException e) {
- // ignore
- }
- }
- @RunWith(SpringJUnit4ClassRunner.class)
- @ContextConfiguration("/spring-test.xml")
- public class MyTest {
- }
- public class DatabaseTest implements ApplicationContextAware {
- private ApplicationContext context;
- private DataSource source;
- public void setApplicationContext(ApplicationContext applicationContext) {
- this.context = applicationContext;
- }
- @Before
- public void before() {
- source = (DataSource) dataSource.getBean("dataSource", DataSource.class);
- }
- @After
- public void after() {
- source = null;
- }
- }
- public class MyDataSourceSpringTest extends
- AbstractTransactionalDataSourceSpringContextTests {
- @Override
- protected String[] getConfigLocations() {
- return new String[]{"classpath:test-context.xml"};
- }
- @Override
- protected void onSetUpInTransaction() throws Exception {
- super.deleteFromTables(new String[]{"myTable"});
- super.executeSqlScript("file:db/load_data.sql", true);
- }
- }
- public class MyDataSourceSpringTest extends
- AbstractTransactionalDataSourceSpringContextTests {
- @Override
- protected String[] getConfigLocations() {
- return new String[]{"classpath:test-context.xml"};
- }
- @Override
- protected void onSetUpInTransaction() throws Exception {
- super.executeSqlScript("file:db/recreate_tables.sql", true);
- }
- }
- This is what we do at the start of every test.
- 1) Drop all Previous Objects.
- 2) Create all tables mentioned in the create_table.sql
- 3) Insert values onto the created tables based on what you want to test.
- @Before
- public void initialInMemoryDatabase() throws IOException, FileNotFoundException {
- inMemoryDerbyDatabase.dropAllObjects();
- inMemoryDerbyDatabase.executeSqlFile("/create_table_policy_version_manager.sql");
- inMemoryDerbyDatabase.executeSqlFile("/insert_table_policy_version_manager.sql");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement