Mockito doAnswer() not returning the updated Map

301 views
Skip to first unread message

eswaramoorthy sabapathy

unread,
Jun 8, 2015, 7:36:34 PM6/8/15
to moc...@googlegroups.com

hi - I am trying to use doAnswer() to stub the void method to call MyBatis mapper interface.

Mockito.doAnswer(new Answer()
   {
    @Override
    public Object answer(InvocationOnMock invocation) throws Throwable {
        Map<String, String> input = (Map<String, String>) invocation.getArguments()[0];
        input.put("NEW_PARAM","VALUE");
        return input;
    }}).when(mockDAOLayer).getData(inputMap);


so I am trying to get the Input map that gets passed to the DAOLayer.getData() method and return the map with one additional parameter in it back to the real object.

with real objects, this map would get updated by MyBatis interface with the additional parameter after the DB Stored Procedure call.

Brice Dutheil

unread,
Jul 18, 2015, 11:04:26 AM7/18/15
to moc...@googlegroups.com
Hi,

This design feels wrong, are you sure the DAO returns the very same map instance ?

If not I strongly advice to code it this way :

`when(mockDaoLayer.getData(anyMap())).thenReturn(the_populated_map)`

If you really need to return the very same map instance, I'd use the `anyMap` matcher instead of a reference `inputMap`. Anyway if this is possible I'd change this code to not rely on instance, but on the content of the collection. 


-- Brice

--
You received this message because you are subscribed to the Google Groups "mockito" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mockito+u...@googlegroups.com.
To post to this group, send email to moc...@googlegroups.com.
Visit this group at http://groups.google.com/group/mockito.
To view this discussion on the web visit https://groups.google.com/d/msgid/mockito/1015f9d5-9477-463d-898d-54743902a804%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages