Jelle den Burger
Jelle den Burger

Reputation: 1468

Run a piece of code once before certain tests

I am currently developing cleanup/migration code for our database. I want to run this code once when one or more of our database tests is ran (we have some non-database tests as well, and it is not needed run the cleanup/migration code).

I have already looked at the @BeforeClass annotation, but it runs for every class. So when I run 10 database tests, the cleanup/migration code will run 10 times as well, which is not really good for performance.

I was not able to find a good solution yet, so does anyone know a good solution?

Thanks.

Upvotes: 1

Views: 46

Answers (1)

TrueDub
TrueDub

Reputation: 5070

Check out DBUnit - it provides exactly what you're looking for - database build & teardown, plus all sorts of cleanup.

http://dbunit.sourceforge.net/

Upvotes: 1

Related Questions