Reputation: 1
I would like write something like this :
this.spyObject = Mockito.spy(myObject);
...
verify(this.spiedObject, atLastOnce()).getMember().doSomething(any(OneClass.class))
or
this.spyObject = Mockito.spy(myObject);
...
verify(this.spiedObject.getMember(), atLastOnce()).doSomething(any(OneClass.class))
in a Junit test, knowing that the constructor of myObject is
public MyObject() {
this.member = new Member()
}
Obviously, it doesn't works !
Finally, I would be able to verify doSomething
was called on the member
.
I'm pretty beginner with Mockito :)
Any help please ?
Upvotes: 0
Views: 372
Reputation: 1
It sounds like you want Member
to be a spy too. In this case, you could do something like this:
@RunWith(MockitoJUnitRunner.class)
public class SomeTestClass {
private MyObject myObjectSpy;
private Member memberSpy;
@Test
public void someTest() {
memberSpy = spy(Member.class);
myObjectSpy = spy(MyObject.class);
doReturn(memberSpy).when(myObjectSpy).getMember();
...
verify(memberSpy, atLeastOnce()).doSomething(any(OneClass.class));
}
}
Be warned, including doReturn(...)
with a spy is partial mocking if you intend to only mock getMember()
and not its other functions. In that case, consider using a full mock and deciding what the return values of its other functions should be.
Upvotes: 0