This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Testing and the fly likes How to set properties on a Mocked Object using EasyMock? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Engineering » Testing
Bookmark "How to set properties on a Mocked Object using EasyMock?" Watch "How to set properties on a Mocked Object using EasyMock?" New topic
Author

How to set properties on a Mocked Object using EasyMock?

Arjun Reddy
Ranch Hand

Joined: Nov 10, 2007
Posts: 627
Hi All,

I have a piece of code as below:



Now from the calling method, I want to set id to a different value. How do I do it?

Thanks

Be Humble... Be Nice.
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30356
    
150

The calling object doesn't know it has a mock. It just makes calls to the object normally. This causes awkwardness if the object you are mocking out has getters and setters as the mock doesn't have logic like this. You would have to set expectations on the mock that you call a sequence like:
1) get() - returns 2
2) set(3)
3) get() - returns 3

You might consider writing your own mock that can store state and just checks if the method you want to mock out is called.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
enric jaen
Greenhorn

Joined: Oct 15, 2010
Posts: 25

You could try



Assuming that your calling method first calls the get and then the set
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to set properties on a Mocked Object using EasyMock?