Java – JUnit Derby spring: the in memory database is discarded after each test
In my unit test, I automatically installed some data sources using URLs
jdbc:derby:memory:mydb;create=true
Create dB in memory@ H_ 502_ 5@
To delete the Derby database in memory, you must connect to: @ h_ 502_ 5@
jdbc:derby:memory:mydb;drop=true
I think this happens after every test and start with a new database How can I do this with spring@ H_ 502_ 5@
Solution
If spring is used with hibernate, there is a database independent method
Ensure that the application context is created / destroyed before / after each test method: @ h_ 502_ 5@
@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 { }
Instructs hibernate to automatically create patterns at startup and delete patterns at shutdown: @ h_ 502_ 5@
hibernate.hbm2ddl.auto = create-drop
Now @ h before each test_ 502_ 5@
>Create the application context and inject the required spring beans (spring) > create the database structure (hibernate) > if it exists, execute import SQL (sleep) @ h_ 502_ 5@
@ h after each test_ 502_ 5@
>Application context destroyed (spring) > delete database mode (hibernate)@ H_ 502_ 5@
If you are using transactions, you may need to add transactionaltestexecutionlistener@ H_ 502_ 5@