How do I check if bar(Alpha, Baz)
called bar(Xray, Baz)
using Mockito - without actually calling the later, given my MCVE class Foo
:
The reason this is driving you nuts is that you aren't using Mockito the way it is intended to be used. It can be used this way, but it should not be.
You don't need to test that the right method was called, you need to test that the behavior of your class is correct. What does that mean?
You want to check that "Xray"
was returned.
String result = foo.bar(alpha, baz);
assertEquals("Xray", result);
You want to check that Alpha.get
was called. If anything, this means that Alpha
should be the stub, not Foo
.
Alpha alpha = spy(new Alpha());
String result = foo.bar(alpha, baz);
verify(alpha).get();
The bottom line is this: You have your System Under Test, in this case Foo
. That class should be a real class. Your mocks and stubs should be the other objects that interact with Foo
; verify that the interactions do the correct things.
In other words, you shouldn't be testing how Foo
works, you should be testing that Foo
does what it's supposed to do; that the results are correct.