简体   繁体   中英

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...

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM