1

I am working on some tests for a project here at my company. I used Mockito.verify(...) in combination with some ArgumentCaptor quite a bit but know I am a bit puzzeled, because in my case the following test code does not work out as expected:

@Mock BuildHandler buildHandler;
...
ArgumentCaptor<Build> savedBuildCaptor = ArgumentCaptor.forClass(Build.class);
verify(buildHandler, times(1)).save(savedBuildCaptor.capture());
...

I expected the savedBuildCaptor to catch me any Build object that is passed to the buildHandler.save(Build b) method. In fact during the execution I end up with this test failure from Mockito:

Argument(s) are different! Wanted: buildHandler.save(null);
Actual invocation has different arguments: buildHandler.save(1234);

Can anyone point me to where I (might have) screwed up?

If additional code/info is required I'll post it.

Daniel Eder
  • 90
  • 1
  • 6
  • Can you explain how this is a duplicate? AFAIK do not use the `ArgumentCaptor` for stubbing. Actually I do exactly the same stuff that is written in the answer of that post. – Daniel Eder Feb 08 '16 at 11:56
  • What's the signature of the `save(...)` method? – acdcjunior Feb 08 '16 at 15:35
  • It's an interface method defined as `public void save(Build build) throws DatabaseException;` – Daniel Eder Feb 08 '16 at 16:48
  • If you take the `times(1)` argument out, does it work? – acdcjunior Feb 08 '16 at 17:37
  • 1
    Indeed it does. Which I think is strange but anyway. I did not try this. If you post it as answer I'd mark it as solved! – Daniel Eder Feb 09 '16 at 11:52
  • 1
    This is, as you say, very strange. What version of Mockito are you using? I did some tests with the latest (`1.10.19`) and `times(1)` didn't matter... – acdcjunior Feb 09 '16 at 14:18
  • I am using `1.10.19` as well. The strange thing is that it is working with other test cases, but this one is failing when I put hte `times(1)` in place. I think I'll file a bug with `Mockito` maybe they can figure something out. – Daniel Eder Feb 10 '16 at 06:51

0 Answers0