开发者

DbUnit framework to Test DAO layer

开发者 https://www.devze.com 2023-03-26 07:02 出处:网络
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.

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消