Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DAO Unit testing

Tags:

I have been looking at EasyMock and tutorials/examples around using it for Unit Testing DAO classes, for an "outside container" test. However, I think most of them talk about testing the Service Layer instead, mocking the DAO class. I am a bit confused, is it really how you Unit Test the DAO layer?

Some would say that the tests interacting with DB & EJBs are actually Integration tests and not Unit tests but then how would you know if your SQL is correct (assuming no ORM) and your DAO inserts/queries the right data from your real (read, local database which is similar to that in production) database?

I read that DBUnit is a solution for such a situation. But my question is about using a framework like DBUnit "outside container". What if the DAO depends on some EJBs, how do we handle the transactions, what happens if there are triggers that update other tables on your inserts?

What is the best way to Unit Test only the DAOs with such dependencies?

like image 254
Dchucks Avatar asked Mar 21 '12 15:03

Dchucks


People also ask

How you should unit test DAO layer?

1) Always create unit test specific configuration file This may be the first step for creating unit tests for your DAO layer. Ideally, you should be using same configuration for tests as you are using for application. But there may be some changes which are only unit test specific.

How do you test DAO?

Enzyme test: Dao is an analytical test which is carried out in the laboratory using the ELISA method to measure the level of the DAO enzyme in the blood and thus to identify whether the migraine is caused by a deficit in DAO. You should fast for a minimum of eight hours prior to the extraction of blood.

Should you unit test Data Access Layer?

It is a good practice to write unit test for every layer, even the DAL.

How do you write JUnit test cases for Hibernate DAO?

The best way to test your dao layer is to use the spring jdbctemplate to write data to your database the test your get and delete methods. Then in the @after delete the records you wrote. Then use hibernate to write to your database and use jdbctemplate to read them back. Then delete your test rows.


1 Answers

Personally, I unit test DAOs by hitting some sort of test database, preferable the same type of database (not the SAME database, obviously) that your app uses in production.

I think if you do that, the test is more of an integration test, because it has a dependency on a running database. This approach has the benefit in that it is as close as possible to your running production environment. It has the downsides that you need test configuration, you need a running test database (either local to your machine or somewhere in your environment) and the tests can take longer to run. You also need to be sure to rollback the test data after tests execute.

Once DAOs are tested, definitely mock them to unit test your services.

like image 98
hvgotcodes Avatar answered Sep 19 '22 12:09

hvgotcodes