Database base unit testing strategy: truncating tables between unit tests & test data

那年仲夏 提交于 2019-12-24 14:45:44

问题


I'm attempting to use JUnit to test some database code. I'm pretty new to this so please bear with me.

I have a local test database which consists of four tables. Two of these tables are populated with data and the other two are empty before the program executes.

The program basically performs some queries on the two tables which are populated, processes the results then writes those results to the two tables which are empty when the program is executed. There are a number of methods which read and write to the db. I would like to test these.

What I would like is for the tearDown() method to truncate the tables which are empty when the program executes so that they are ready for the next test. What is the best way of achieving this?

Also, the tables which contain the data will probably be created by running SQL script each time a developer sets up a test environment. Is there a better way of doing this? Could it be automated? There will be quite a lot of data so I don't really fancy manually creating XML datasets for each table.

I'm using an Oracle database.

Many thanks for any advice/suggestions you can offer.


回答1:


Try using the DBUnit framework in your tests. This is an extension to JUnit and provides the functionality you need. Setting it up can be a bit hard, but once set up it works like a charm.



来源:https://stackoverflow.com/questions/9110726/database-base-unit-testing-strategy-truncating-tables-between-unit-tests-test

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!