Jiew Meng
Jiew Meng

Reputation: 88197

How to have a single setUp tearDown for a whole group of JUnit tests?

I want to setup my testing database with test data before I start my tests. I suppose I should run that once at the start of the unit tests instead of before each test class for function? How might I do that?

Upvotes: 0

Views: 729

Answers (3)

Cedric Beust
Cedric Beust

Reputation: 15608

For what it's worth, TestNG supports this with @BeforeSuite and @AfterSuite (and many more configuration annotations).

Upvotes: 0

Kkkev
Kkkev

Reputation: 4895

The accepted solution to How to load DBUnit test data once per case with Spring Test will do this. It works across an arbitrary set of test cases.

Upvotes: 1

k.m
k.m

Reputation: 31454

You can achieve that with @SuiteClasses annotation:

@RunWith(Suite.class)
@SuiteClasses({UserDaoTests.class, OrderDaoTests.class})
public class TestSuiteSetup {
    @BeforeClass
    public static void setUpDatabase() {
        // ...
    }

    @AfterClass
    public static void tearDownDatabase() {
        // ...
    }
}

Tests from UserDaoTests and OrderDaoTests will be run between setUpDatabase and tearDownDatabase methods.

Upvotes: 2

Related Questions