5

I am trying to define when mockito method with multiple any arguments:

TestBedDaoClient testBedDaoClient = mock(TestBedDaoClient.class);
when(testBedDaoClient.addTestBed(anyString(), anyString(), any(VCloudConfiguration.class))).thenReturn(testBedPojoMockData);

In target test class:

TestBedPojo addedTestBedPojo = testBedDaoClient.addTestBed(testBedName, testBedDescription, vCloudConfiguration);

In DAO client:

public TestBedPojo addTestBed(String testBedName, String testBedDescription, VCloudConfiguration vCloudConfiguration){
     return testBedPojo;
}

I wanted to define when in such a way that it returns testBedPojoMockData with any values of arguments. But I am getting error: Argument(s) are different!

I even tried:

when(testBedDaoClient.addTestBed("test", "test", any(VCloudConfiguration.class))).thenReturn(testBedPojoMockData);
when(testBedDaoClient.addTestBed(any(), any(), any())).thenReturn(testBedPojoMockData);

But no luck. How I can define this when so that it returns the mock data on any call?

Shashi Ranjan
  • 1,491
  • 6
  • 27
  • 52
  • Can you please paste the `addTestBed()` signature? Also, can you show us, how you define `testBedPojoMockData`? Which is the problematic line according to the exception? – Tamas Rev Oct 03 '16 at 11:47
  • TestBedPojo , I have setters and setters in TestBedPojo, and setting the mock data for TestBedPojo in TestBedPojoMockData class and getting TestBedPojo as return type from TestBedPojoMockData. TestBedPojo is the signature both for `addTestBed()` and `testBedPojoMockData`. Actually I want to get the mockdata on every call(ie: independent of value argument). – Shashi Ranjan Oct 03 '16 at 11:50
  • As said, can you edit your post and add the signature of the method you are trying to mock ? – TheBakker Oct 03 '16 at 11:53
  • Didn't you forget your RunWith annotation? – tmn4jq Oct 03 '16 at 12:02
  • We need the method signature : public TestBedPojo addTestBed( .... – TheBakker Oct 03 '16 at 12:02
  • 1
    "Arguments are different" is an error usually from `verify`. Does not mean that your `when` isn't working (when usually just returns null when arguments doesn't match). Also, I see no `verify`in your code examples... – Tobb Oct 03 '16 at 12:03
  • Thanks @Tobb! `verify` was the issue. – Shashi Ranjan Oct 03 '16 at 12:08
  • Be careful, by the way, for your "I even tried": The first `when` mixes matcher arguments with non-matcher arguments. You have to stick with all-matchers or all-values for any particular `when` or `verify` invocation. – Jeff Bowman Oct 03 '16 at 23:29
  • Yes! @JeffBowman , that I realize after multiple run. Is that a bug? – Shashi Ranjan Oct 04 '16 at 06:29
  • No, not quite a bug; it's a well-documented rule (see [Mockito JavaScript section 3's warning](http://site.mockito.org/mockito/docs/current/org/mockito/Mockito.html#3)) that is a consequence of Matcher design and syntax. I wrote about those implementation details in a [separate SO answer](http://stackoverflow.com/q/22822512/1426891). – Jeff Bowman Oct 04 '16 at 14:08

1 Answers1

5

The correct combination of when and verify should be used. It's failing on any other combination of argument in addTestBed method.

when(testBedDaoClient.addTestBed(anyString(), anyString(), any(VCloudConfiguration.class))).thenReturn(testBedPojoMockData);
//calling target method
verify(testBedDaoClient, times(1)).addTestBed(anyString(), anyString(), any(VCloudConfiguration.class));
Shashi Ranjan
  • 1,491
  • 6
  • 27
  • 52