我有以下 Java 类:
public class A
{
@Autowired
private B b;
public int aFn()
{
int something = b.bFn();
}
}
public class B
{
@Autowired
private C c;
public int bFn()
{
int something = c.cFn();
}
}
public class C
{
public int cFn()
{
return 231;
}
}
以及下面的测试使用 Mockito 来测试上面的代码:
public class test
{
@Autowired
private A a;
private C c;
@Test
public void testA()
{
c = mock(C.class);
when(c.cFn(),anyInt()).thenReturn(something);
assertEquals(0, a.aFn());
}
}
当我调试 testA 时,我发现真正的 c.Cfn() 被执行,而不是被模拟的。我在这里做错了什么吗?请帮忙!