简体   繁体   中英

DbUnit framework to Test DAO layer

Generally we are giving input file from setup() for inserting data so it will insert data every time for every test and finally clear it up after test.

But these data is for all tests and it becomes very much overhead if number of tests are more. So if there is any way that I can give Test specific input(xml) file so that that particular file will be inserted by DbUnit from setUp() so there would be less overhead.

Any suggestion is appreciated. Thanks.

I notice you tagged Spring, you can write a org.springframework.test.context.TestExecutionListener to do what you require (extend AbstractTestExecutionListener ).

In beforeTestMethod(...) load the data using DbUnit. Use the TestContext to retrieve information regarding the testClass or testMethod and decide based on that what dataset to load. For instance you can create a new annotation to specify which dataset to load for a test method.

Unload the dataset in afterTestMethod(...) (if not using transactional tests and data inserted as part of transaction).

Register your TestExecutionListener using the @TestExecutionListeners annotation (important to specify it after TransactionalTestExecutionListener if you are using it and want the dataset insertion to form part of the transaction).

Hope this helps.

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