5

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();
}
zmanc
  • 5,201
  • 12
  • 45
  • 90

1 Answers1

7

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.

Glen Best
  • 22,769
  • 3
  • 58
  • 74