So I'm trying to mock a method call inside another method, here's the pseudo code
class A{ public String getAName(String id){ B b = new B(); // do something return b.getBName(id); } } testgetName{ public void testA(){ B mockB = mock(B.class); Mockito.doReturn("Bar").when(mockB).getBName(id); A a = new A(); a.getAName(id); //this still calls "b.getBName(id)" in class implementation } } The problem here is a.getAName still calls b.getBName(id) - not sure why?
Any suggestions on how should I mock b.getBName(id) properly
Thanks
new, since you have no control over those objects. You would have to make theBa field of classAand then inject it during your test. This is one of the reasons why people like dependency injection :)// do something. I will if this is a method call that takes B, to mock this method, or even better, use an ArugmentCaptor to capture theBpassed to the method. This all hinges on what// do somethingactually does