Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Mock of a method for JUnit testing

 
Daniel Afonso
Greenhorn
Posts: 16
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 989
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so then getThing just returns any thing? In that case


?
You probably need to populate the Thing with some mock values.
 
Daniel Afonso
Greenhorn
Posts: 16
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But I populate the Things on my Test class then right??
 
E Armitage
Rancher
Posts: 989
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 7778
62
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic