Nested method mocking in Mockito
First of all, you should always mock the direct dependencies of an object, and not its transitive dependencies. So you should mock B, and not C, to test A. Then you would write a unit test for B by mocking C.
Second: you're not injecting the mock anywhere in the unit test. What you should have is:
public class Test {
// not autowired here
private A a;
private B mockB;
@Test
public void testA() {
mockB = mock(B.class);
when(b.bFn(), anyInt()).thenReturn(something);
// the missing part: injecting the mock into the tested object
a = new A(mockB);
// or a = new A();
// a.setB(mockB);
assertEquals(0, a.aFn());
}
}
When you use mock(B.class), you get one mock instance of B. That doesn't mean that all the other instances of B will do what the mock does.
Mocking C to test A is a bad practice: unit tests should test one class in isolation of the others. But if you really want that, then create a mock C, create a B and inject the mock C inside it, then create an A and inject the B inside it.
A --> B --> mockC