jQuery in Action, 3rd edition
The moose likes Testing and the fly likes Mock GenericDao for Unit Testing Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » Testing
Bookmark "Mock GenericDao for Unit Testing" Watch "Mock GenericDao for Unit Testing" New topic

Mock GenericDao for Unit Testing

Daniel Afonso

Joined: Jan 27, 2011
Posts: 16

I want to develop a Mock of My GenericDao class. For example for the following method of my GenericDao class:

I have the following Mock:

How can I mock the following method of my GenericDao?? :

Something inside this:

Jeanne Boyarsky
author & internet detective

Joined: May 26, 2003
Posts: 33106

I don't understand the question. What is the connection between getList() and getListByPage()?

[OCA 8 book] [Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2
Junilu Lacar

Joined: Feb 26, 2001
Posts: 6529

I'll take it a step beyond where Jeanne is going: I've seen things like this before and if you're going where I think you're going with this GenericDao thing, while you think that it will simplify things for you, it will only lead you down the road to over-complexity and sorrow.

Here's my suggestion:
  • Use interfaces to define the contracts for your DAO. If you read Eric Evans' book "Domain Driven Design", a DAO is really just another type of service: an infrastructure service to access a Repository.
  • Using interfaces to define the contract for your DAO/RepositoryService allows you to use mocking frameworks like Mokito - learn how to use those, they are full of goodness and joy

  • When it comes to DAOs, it's better to be very specific about what they do, rather than trying to make some kind of "generic" thing that you can somehow configure. There is always going to be some variation that you didn't think of before that will cause you to make changes to the "generic" DAO that is very specific to each newly-thought-of case. Very ironic. There are frameworks like Spring and Hibernate that provide a lot of ready-to-use classes that will simplify things for you at the "plumbing" level of code so that you can concentrate on writing the business logic instead. Learn how to use frameworks like Spring and ORM frameworks like Hibernate to simplify your code. A lot of the heavy lifting has already been done for you. They are also full of goodness and joy, when used properly.

    If my read on your direction is wrong and you're only calling it GenericDao in the same sense as say, FooDao, the suggestions 1 and 2 above are still in effect. Additional thought: paging is a presentation concern and should be separated from the concern of getting data out of a repository. As such, a method like getListByPage creates a code smell in a DAO class. See this article http://thinkbeforecoding.com/post/2009/04/08/Back-on-Repositories-and-Paging-Introducing-reporting for more.

    Junilu - [How to Ask Questions] [How to Answer Questions]
    I agree. Here's the link: http://aspose.com/file-tools
    subject: Mock GenericDao for Unit Testing
    It's not a secret anymore!