London
London

Reputation: 15274

Writing tests for DAOs

I'm currently assigned to write a test for a project, is it necessary to write tests for DAO classes?

Upvotes: 12

Views: 1562

Answers (7)

ams
ams

Reputation: 62632

The book xUnit Test Patterns offers a lot of great insights into this very question.

Upvotes: 0

Bino Manjasseril
Bino Manjasseril

Reputation: 112

I would argue that we should write unit tests for DAOs and one of the biggest challenge to do it is the test data setup and cleanup. That is where I think, frameworks, such as Spring JDBC testing framework can help us out by letting us control the transaction using different annotations [Example: @Rollback(true)].

For example, if you are testing a "create/insert" operation, Spring allows you to completly rollback the transaction after the execution of the test method, thereby leaving the database in its original state always.

You may take a look at this link for more information: Spring Testing

This can be even more useful when for your integration tests where you don't want one test to spoil the data integrity, which can cause another test to fail.

Upvotes: 0

Sid
Sid

Reputation: 4997

Yes. There are several benefits of doing so. Once you are sure that your DAO layer is working fine, defect fixing in later stages becomes easy.

Upvotes: 0

Péter Török
Péter Török

Reputation: 116256

It depends :-)

If your DAO classes contain only the code needed to fetch entities from the DB, it is better to test them in separate integration tests*. The code to be unit tested is the "business logic" which you can unit test using mock DAOs.

[Update] E.g. with EasyMock you can easily set up a mock for a specific class (with its class extension, even concrete classes can be mocked), configure it to return a specific object from a certain method call, and inject it into your class to be tested.

The EasyMock website seems to be down right now, hopefully it will come back soon - then you can check the documentation, which is IMHO quite clean and thorough, with lots of code examples. Without much details in your question, I can't give a more concrete answer. [/Update]

If, OTOH, the DAOs contain also business logic, your best choice - if you can do that - would be to refactor them and move the business logic out of DAOs, then you can apply the previous strategy.

But the bottom line is, always keep in mind the unit testing motto "test everything which could possibly break". In other words, we need to prioritize our tasks and concentrate our efforts on writing the tests which provide the most benefit with the least effort. Write unit tests for the most critical, most bug-risky code parts first. Code which - in your view - is so simple it can't possibly break is further down the list. Of course it is advisable to consult with experienced developers on concrete pieces of code - they may know and notice possible traps and problems you aren't aware of.

* unit tests are supposed to be lightweight, fast and isolated from the environment as much as possible. Therefore tests which include calls to a real DB are not unit tests but integration tests. Even though technically they can be built and executed with JUnit (and e.g. DbUnit), they are much more complex and orders of magnitude slower than genuine unit tests. Sometimes this makes them unsuitable to be executed after every small code change, as regular unit tests could (and often should) be used.

Upvotes: 7

b.roth
b.roth

Reputation: 9532

Yes, you should write unit tests for DAO's.

These unit tests can use an in-memory database. See for example: HyperSQL

Article on how to use HyperSQL to write persistence unit tests in Java:

http://www.mikebosch.com/?p=8

Upvotes: 4

Adeel Ansari
Adeel Ansari

Reputation: 39887

Yes. But few folks would argue that, it doesn't come into the category of unit tests. Because it will not conform to the definition of unit test per say. We call it integration test where we test the integration of code to the database.

Moreover, I agree to the idea of Bruno here. Further, there are APIs available just to do that, one is DBUnit.

Upvotes: 4

David M
David M

Reputation: 72840

It's not necessary to write tests for anything. Do you get benefit from writing tests for your DAO classes? Probably.

Upvotes: 0

Related Questions