Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

unit testing DAOs

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 call my deletion method?

like image 610
Glide Avatar asked Mar 23 '26 12:03

Glide


2 Answers

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.

like image 130
manuel aldana Avatar answered Mar 25 '26 12:03

manuel aldana


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.

like image 24
Nathan Hughes Avatar answered Mar 25 '26 12:03

Nathan Hughes



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!