Advertisement
Guest User

Untitled

a guest
May 6th, 2016
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.72 KB | None | 0 0
  1. jdbc:derby:memory:mydb;create=true
  2.  
  3. jdbc:derby:memory:mydb;drop=true
  4.  
  5. @RunWith(SpringJUnit4ClassRunner.class)
  6. @ContextConfiguration({"classpath*:application-context-test.xml"})
  7. @TestExecutionListeners({DirtiesContextTestExecutionListener.class,
  8. DependencyInjectionTestExecutionListener.class})
  9. @DirtiesContext(classMode = ClassMode.AFTER_EACH_TEST_METHOD)
  10. public abstract class AbstractTest {
  11.  
  12. }
  13.  
  14. hibernate.hbm2ddl.auto = create-drop
  15.  
  16. mydb.drop.url = jdbc:derby:memory:mydb;drop=true
  17.  
  18. ...
  19.  
  20. <bean id="mydbDropUrl" class="java.lang.String">
  21. <constructor-arg value="${mydb.drop.url}" />
  22. </bean>
  23.  
  24. ...
  25.  
  26. @Resource
  27. private String mydbDropUrl;
  28.  
  29. @After
  30. public void tearDown() {
  31. try {
  32. DriverManager.getConnection(mydbDropUrl);
  33. } catch (SQLException e) {
  34. // ignore
  35. }
  36. }
  37.  
  38. @RunWith(SpringJUnit4ClassRunner.class)
  39. @ContextConfiguration("/spring-test.xml")
  40. public class MyTest {
  41. }
  42.  
  43. public class DatabaseTest implements ApplicationContextAware {
  44. private ApplicationContext context;
  45. private DataSource source;
  46.  
  47. public void setApplicationContext(ApplicationContext applicationContext) {
  48. this.context = applicationContext;
  49. }
  50.  
  51. @Before
  52. public void before() {
  53. source = (DataSource) dataSource.getBean("dataSource", DataSource.class);
  54. }
  55.  
  56. @After
  57. public void after() {
  58. source = null;
  59. }
  60. }
  61.  
  62. public class MyDataSourceSpringTest extends
  63. AbstractTransactionalDataSourceSpringContextTests {
  64.  
  65. @Override
  66. protected String[] getConfigLocations() {
  67. return new String[]{"classpath:test-context.xml"};
  68. }
  69.  
  70. @Override
  71. protected void onSetUpInTransaction() throws Exception {
  72. super.deleteFromTables(new String[]{"myTable"});
  73. super.executeSqlScript("file:db/load_data.sql", true);
  74. }
  75. }
  76.  
  77. public class MyDataSourceSpringTest extends
  78. AbstractTransactionalDataSourceSpringContextTests {
  79.  
  80. @Override
  81. protected String[] getConfigLocations() {
  82. return new String[]{"classpath:test-context.xml"};
  83. }
  84.  
  85. @Override
  86. protected void onSetUpInTransaction() throws Exception {
  87. super.executeSqlScript("file:db/recreate_tables.sql", true);
  88. }
  89. }
  90.  
  91. This is what we do at the start of every test.
  92.  
  93. 1) Drop all Previous Objects.
  94. 2) Create all tables mentioned in the create_table.sql
  95. 3) Insert values onto the created tables based on what you want to test.
  96.  
  97. @Before
  98. public void initialInMemoryDatabase() throws IOException, FileNotFoundException {
  99.  
  100. inMemoryDerbyDatabase.dropAllObjects();
  101. inMemoryDerbyDatabase.executeSqlFile("/create_table_policy_version_manager.sql");
  102. inMemoryDerbyDatabase.executeSqlFile("/insert_table_policy_version_manager.sql");
  103.  
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement