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 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
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: 629
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
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 31079
    
163

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
 
Don't get me started about those stupid light bulbs.
 
subject: How to set properties on a Mocked Object using EasyMock?