This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Mock of a method for JUnit testing  RSS feed

 
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
Sheriff
Posts: 11142
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac 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
Boost this thread!