Currently I am using the below after every test I have in my test suite. However it makes it very slow because H2 has to reload the application context after every test. Is there a quicker way to clear all my objects so I do not have carry over between tests?
@org.junit.After
public void tearDown() throws Exception {
context.close();
}
Try context.clear()
EntityManager.clear: Clear the persistence context, causing all managed entities to become detached. Changes made to entities that have not been flushed to the database will not be persisted.
Session.clear: Completely clear the session.
Also, you can ensure the entity manager / session used in each test is identical by implementing a singleton pattern. And you can use a dynamic SQL script to clear data from all tables. Both described under PersistenceHelper here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With