File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Testing and the fly likes Mock method to JUnit test Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » Testing
Bookmark "Mock method to JUnit test" Watch "Mock method to JUnit test" New topic
Author

Mock method to JUnit test

Daniel Afonso
Greenhorn

Joined: Jan 27, 2011
Posts: 16

Hi have the following method:



For this previous method I have the following Mock:






Now I have the following method:





How can I mock this method?? I did the following but I'm most certainly sure that this is not correct. Because of the parameter "id". I'm not using it,


Greg Charles
Sheriff

Joined: Oct 01, 2001
Posts: 2849
    
  11

I'm not clear what you're trying to do. You want to create a mock, such that calls to getList(int) in the code you're testing actually call getList() with no parameters? I don't think that's possible, but of course you can mock getList(int) and have it call getList().
Daniel Afonso
Greenhorn

Joined: Jan 27, 2011
Posts: 16

I edited the topic.
Greg Charles
Sheriff

Joined: Oct 01, 2001
Posts: 2849
    
  11

If your mock's purpose is to bypass the database, then you have to get the data from somewhere else. In your first mock, you load it up from a "things" object, but you don't show where that object is created or populated. In any case, I would have your second method go through "things", pull out the members that match "id", and then return those as a list.
Daniel Afonso
Greenhorn

Joined: Jan 27, 2011
Posts: 16

Then my approach in the second method is correct?
Greg Charles
Sheriff

Joined: Oct 01, 2001
Posts: 2849
    
  11

Well, mocks are used for testing, so if it adequately tests your logic, then it is correct in that sense. I assumed that you would want to filter "things" by the id parameter to better mimic what the original method does. Your second mock doesn't do that, so in the sense of following my suggestion, no, it isn't correct.
Daniel Afonso
Greenhorn

Joined: Jan 27, 2011
Posts: 16

How would you implemented this method filtering it by id?

thanks, i'm a rookie in tests using mocks
dennis deems
Ranch Hand

Joined: Mar 12, 2011
Posts: 808
What is your test case? Is it really necessary for your mock to filter a bunch of test "things" in a manner analogous to what is done by the working application? If so, it should be a trivial matter as Greg indicated to iterate over the map's contents and return those things which have the required id. However you might consider whether perhaps your test is attempting to handle more responsibility than is necessary. If the DAO layer has its own suite of tests that cover this same logic, then really it ought to be sufficient to verify that a call was made to the DAO, and that Something was returned by the service. (Of course if the service has additional logic to perform, the test should cover that too.)
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Mock method to JUnit test