您正在将a
Mock与混淆
Spy。
在模拟中, 所有方法 都存根并返回“智能返回类型”。这意味着,除非指定行为,否则在模拟类上调用任何方法都不会 执行任何操作 。
在间谍中,该类的原始功能仍然存在,但是您可以在间谍中验证方法调用,也可以覆盖方法行为。
你想要的是
MyProcessingAgent mockMyAgent = Mockito.spy(MyProcessingAgent.class);
一个简单的例子:
static class TestClass { public String getThing() { return "Thing"; } public String getOtherThing() { return getThing(); }}public static void main(String[] args) { final TestClass testClass = Mockito.spy(new TestClass()); Mockito.when(testClass.getThing()).thenReturn("Some Other thing"); System.out.println(testClass.getOtherThing());}输出为:
Some Other thing
注意:您应该真正尝试模拟正在测试的类的依赖关系,而 不是 类本身。



