someone4746
someone4746

Reputation: 139

JUnit setup for all tests

I need to setup a database in my tests (schema and some test data), this takes quite a bit of time, and as such I prefer to have it done once for all tests that are being run, and reset so that any chanages to the DB are rolled back between tests.

I'm not sure though which JUnit facilities should be used for this. It seems like I can set a @BeforeClass/@AfterClass on a test suite, but than I can't run individual tests anymore. Is there some way to add a setup/teardown for all tests that will run even when only executing a subset of the tests and not a specific suite? (For example NUnit has SetUpFixture) I guess the transactions/truncation of the DB can be done using JUnit Rules...

Upvotes: 2

Views: 331

Answers (1)

TheKojuEffect
TheKojuEffect

Reputation: 21081

You can use in-memory databases like HSQL or H2 to speed up test.

To roll back, you can use transactional feature.

Is there some way to add a setup/teardown for all tests that will run even when only executing a subset of the tests and not a specific suite?

For this, you can create a super class which is extended by other test classes. In super class, you can set up to setup/teardown.

Upvotes: 1

Related Questions