aspose file tools*
The moose likes Testing and the fly likes Mockito Beginner Question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Engineering » Testing
Bookmark "Mockito Beginner Question" Watch "Mockito Beginner Question" New topic
Author

Mockito Beginner Question

yusuf Kaplan
Ranch Hand

Joined: Mar 11, 2011
Posts: 58
Hi all,

following code:


returns 0 in Console. But I expect it to return 50.
ServiceImpl looks like:


and CustomerDAOImpl


Any suggestions?
Sresh Rangi
Ranch Hand

Joined: Nov 28, 2012
Posts: 39
    
    1
The service method is calling count, which is mocked to return 0 by default. In your test, you should mock the count method instead of the findAll method.
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4419
    
    5

To me, this flags a problem with the production code. Notice how the Service.count() method is simply a pass-through call to the DAO.count() method -- this construct is just useless overhead. I would rethink the purpose of having a count() method in the service class. Maybe it would be more useful to have something like this instead:


or something else that uses the size of the list to calculate a more abstract concept such as "requiresMultipleSalesRegions" or "belowMinimumRequiredLevel"


Junilu - [How to Ask Questions] [How to Answer Questions]
yusuf Kaplan
Ranch Hand

Joined: Mar 11, 2011
Posts: 58
Hi Sresh,

thanks for the answer.
ServiceImpl.count calls CustomerDAOImpl.count calls CustomerDAOImpl.findAll.
When my test runs into CustomerDAOImpl.findAll it should return the list with 50 entries.
I explicitly dont want to mock count(). I just want a method to be mocked which is deeper in the call hierarchy.

Sresh Rangi wrote:The service method is calling count, which is mocked to return 0 by default. In your test, you should mock the count method instead of the findAll method.
Sresh Rangi
Ranch Hand

Joined: Nov 28, 2012
Posts: 39
    
    1
With mocks, the call hierarchy ends when it reaches the mocked interface. To use the real implementation for one method and mock another, you can use partial mocking. Mockito can do this with spies:



or you could subclass CustomerDAOImpl and override findAll to return your list.

Spies are discouraged though, because you can usually redesign the program or the tests to not require partial mocking. This results in a clearer boundary between code under test, and dependent components.
yusuf Kaplan
Ranch Hand

Joined: Mar 11, 2011
Posts: 58
Got it.
Partial Mocking solved the problem.
Thanks for everybody
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Mockito Beginner Question
 
Similar Threads
StrutsPrepareandExecutefilter error
Unable to understand exception
passing class name rather then object
How to add JPA ManyToMany records?
BeanPropertyRowMapper returning mostly nulls in returned objects' fields