user592748
user592748

Reputation: 1234

Using Mockito spy to mock a method without specifying the exact argument

I have a use case where I have to test the real method which calls a method inside. This inner method has to be mocked. For instance,

Class Sample {
   boolean method(Foo foo) {
     return innerMethod(new Goo(foo));
   }
}

So I want to do the following.

Sample sample = Mockito.spy(new Sample());
Foo foo = new Foo();
doReturn(false).when(sample).innerMethod(new Goo(foo));

assertEquals(false, sample.method(foo));

The problem is, I suppose, the inner method is never mocked since the arguments Goo are not the same objects. How do I get around this problem?

Upvotes: 0

Views: 1982

Answers (1)

BetaRide
BetaRide

Reputation: 16894

Use the Matcher.any() method.

doReturn(false).when(sample).innerMethod(Matcher.any(Goo.class));

Upvotes: 4

Related Questions