Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Proper tearDown Hibernate testing with H2

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();
}
like image 834
zmanc Avatar asked Apr 23 '13 14:04

zmanc


1 Answers

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.

like image 155
Glen Best Avatar answered Oct 13 '22 14:10

Glen Best