aspose file tools*
The moose likes Testing and the fly likes Mock of a method for JUnit testing Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » Testing
Bookmark "Mock of a method for JUnit testing" Watch "Mock of a method for JUnit testing" New topic
Author

Mock of a method for JUnit testing

Daniel Afonso
Greenhorn

Joined: Jan 27, 2011
Posts: 16

I want to write a mock of a method. For example, for the following method:




I have the following Mock:




Where "things" is a HashMap.

Now, how can I mock the following method? :



Something inside this:



Where Thing is a model of a Object from a database.
E Armitage
Rancher

Joined: Mar 17, 2012
Posts: 892
    
    9
so then getThing just returns any thing? In that case


?
You probably need to populate the Thing with some mock values.
Daniel Afonso
Greenhorn

Joined: Jan 27, 2011
Posts: 16

But I populate the Things on my Test class then right??
E Armitage
Rancher

Joined: Mar 17, 2012
Posts: 892
    
    9
Daniel Afonso wrote:But I populate the Things on my Test class then right??

In your mock class. You can mock out the whole data access layer so you have one mock for your dao.
IMO, it's much better to do away with mocks and use tools like arquillian so your tests run against a test/mock database rather than mocking your classes.
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4462
    
    6

The ensuing discussion regarding mocks vs no mocks has been split off to a new topic so that it doesn't detract from the original question/post. Thanks.


Junilu - [How to Ask Questions] [How to Answer Questions]
 
Don't get me started about those stupid light bulbs.
 
subject: Mock of a method for JUnit testing