0

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?

Jiew Meng
  • 84,767
  • 185
  • 495
  • 805

3 Answers3

2

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.

k.m
  • 30,794
  • 10
  • 62
  • 86
  • If I leave `@RunWith`, I get `Class not found`. If I remove that, I get no tests found, but I have 4 `@Test` in `DataAccessTests`. Please refer to my other question http://stackoverflow.com/questions/9627434/classnotfoundexception-when-in-junit-suite-setup-class – Jiew Meng Mar 09 '12 at 01:17
1

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.

Community
  • 1
  • 1
Kkkev
  • 4,716
  • 5
  • 27
  • 43
0

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

Cedric Beust
  • 15,480
  • 2
  • 55
  • 55