Re: [mockito] How to retrieve a data value in a mocked object that was set by its setter?

4,012 views
Skip to first unread message

Eric Lefevre-Ardant

unread,
Sep 6, 2012, 2:55:34 AM9/6/12
to moc...@googlegroups.com
You can do that (somewhat painfully) by using an Answer:
doAnswer(new Answer() {...}).when(asset).setData(any(String.class))
doAnswer(new Answer() {...}).when(asset).getData()

but, really, I don't quite see the benefits on Mockito here. You would be much better off just creating a local implementation of these signatures.

On 6 September 2012 03:11, Z <tiger...@gmail.com> wrote:
Hi, Here is the object to be mocked:

public class Asset {
   String getData();
   void setData(String data);
}

In my business code (Repository.java), Asset.setData() is called to populate its data. In my test driver (RepositoryTest.java), I hope to test my business code using a mocked Asset object. Is there a way that I can call Asset.getData() to get the data that is set by the setter, setData()? Keep in mind that the setter is called in my business code. I wonder if I can mock Asset in my test code such that I can save the data value somewhere when setter is called. Then I can return it when the getter is called. Something like the following:

Asset asset = Mockito.mock(Asset.class);
String savedData;
Mockito.when(asset.setData($VALUE)).thenSaveTo(savedData);
Mockito.when(asset.getData()).thenReturn(savedData);

Any help is appreciated.

--
You received this message because you are subscribed to the Google Groups "mockito" group.
To view this discussion on the web visit https://groups.google.com/d/msg/mockito/-/sB7nqm6j5hgJ.
To post to this group, send email to moc...@googlegroups.com.
To unsubscribe from this group, send email to mockito+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/mockito?hl=en.

Brice Dutheil

unread,
Sep 6, 2012, 10:34:14 AM9/6/12
to moc...@googlegroups.com
Hi,

I agree with Eric, we discourage mocking Value Object which that exactly what you want to achieve, instead just rewrite your code (production and / or test) so you won't have to mock Asset.

Take a look at this page :)

The book is great by the way!

Cheers,
-- Brice

Eric Lefevre-Ardant

unread,
Sep 6, 2012, 10:56:21 AM9/6/12
to moc...@googlegroups.com
To be fair, I don't think Asset is a Value Object here. It is more like a storage area: save data, read data later.

I think it calls for extracting an interface and providing an implementation in the test. Of course, simply overriding the methods (which is probably what I would do personally) is the poor man's version of introducing an interface.

Brice Dutheil

unread,
Sep 6, 2012, 11:21:38 AM9/6/12
to moc...@googlegroups.com
@Eric Ok, I see what you mean.

@Z Well in this case if you want to still use Mockito for some reasons (like verification), it's possible to use a default answer at mock creation time or just stub both shown methods with your answer as Eric wrote earlier.

Cheers,
-- Brice
Reply all
Reply to author
Forward
0 new messages