How to mock a non static method of an argument class passed to a method? I'm testing below method:
public Seed getAppleSeed(AppleTree appleTree){
Seed appleSeed = appleTree.getApple().getSeed();
//Some logic flow
}
Rest of the classes are below:
public class AppleTree{
public Apple getApple(){
return new Apple():
}
}
public class Apple{
public Seed getSeed(){
return new Seed():
}
}
End goal is to test the flow of the getAppleSeed() method for which I need to mock the calls to getApple and getSeed.
Thanks
With mocking, like this:
AppleTree appleTree = Mockito.mock(AppleTree.class);
Apple apple = Mockito.mock(Apple.class);
Seed seed = new Seed();
Mockito.when(appleTree.getApple()).thenReturn(apple);
Mockito.when(apple.getSeed()).thenReturn(seed);
Seed actual = getAppleSeed(appleTree);
assertThat(actual, is(seed));
Though if the actual code is as simple as what you have outlined in your question then I'd suggest that there is no need to mock Apple
or AppleTree
.