开发者

unit testing DAOs

开发者 https://www.devze.com 2023-01-16 07:22 出处:网络
Let\'s say I\'m doing unit testing methods for a UserDAO. I\'m writing the test for the UserDao\'s deletion method. I would first insert a user into the db, then call the deletion method, and verify i

Let's say I'm doing unit testing methods for a UserDAO. I'm writing the test for the UserDao's deletion method. I would first insert a user into the db, then call the deletion method, and verify if the object still exists.

My question is: for the deletion unit test, when I'm inserting a user to test, should I be calling the UserDao's insertion method...OR would it be better not to call any methods of the object that I'm testing and use the native way, say using jdbc to do the insertion, and then c开发者_如何学JAVAall my deletion method?


DAOs are often too simple to break, then I think it is not worth to spend resources on testing. Your explanation looks like this is the case.

Only if there is some logic involved (building more complex queries together) I would think of testing some parts.

Maybe provide some code snippets to help out more.


Use DBUnit or something similar to set up the data for the test. DBUnit lets you specify what test data gets inserted for a test, you can even specify a clean insert that deletes everything (from the tables that have test data specified for them), then inserts only what you want. It's best if tests are independent of each other, you don't want a problem with the insert code to cause problems for other tests that depend on it for setup.

0

精彩评论

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