java - How do you clear objects between JUnit/Maven tests? -
i'm testing different parts of miniature search engine, , of junit tests leaving entries in index interfere other tests. there convention in junit/maven clearing objects between tests?
there 2 particular annotations can this, , intended used in cases such yours:
@after defines method must executed after every @test, while @afterclass method execute once entire test class has been executed. think of latter last cleanup method purge structures or records you've been using , sharing between tests far.
here example:
@after public void cleanindex() { index.clear(); //assuming have collection } @afterclass public void finalcleanup() { //clean both index and, example, database record. } note: have counterparts (@before , @beforeclass) opposite invoking related methods before @test method , before starting execute @tests defined on class. ones setup used in previous versions of junit.
if can't use annotations, alternative use old teardown method:
public void teardown() { index.clear(); //assuming have collection. } this provided junit framework , behaves method annotated @after.
Comments
Post a Comment