I created a gist ApplicationContextAwareTestBase .
This helper class has two functions:
It sets all internal fields to null. This allows Java to free memory that is no longer in use. This is less useful with Spring (the Spring context still maintains references to all beans).
He tries to find all methods annotated with @After in all beans in context and calling them after the test.
This way you can easily reset the state of your singleton / mocks without having to destroy / update the context.
Example: you have a DAO layout:
public void MockDao implements IDao { private Map<Long, Foo> database = Maps.newHashMap(); @Override public Foo byId( Long id ) { return database.get( id ) ); @Override public void save( Foo foo ) { database.put( foo.getId(), foo ); } @After public void reset() { database.clear(); } }
In the annotation, make sure that reset() is called after each unit test to clear the internal state.
Aaron Digulla Feb 12 '13 at 15:16 2013-02-12 15:16
source share