Mockito 可以处理链接的存根:
Foo mock = mock(Foo.class, RETURNS_DEEP_STUBS);// note that we're stubbing a chain of methods here: getBar().getName()when(mock.getBar().getName()).thenReturn("deep");// note that we're chaining method calls: getBar().getName()assertEquals("deep", mock.getBar().getName());AFAIK,链中的第一个方法返回一个模拟,该模拟被设置为在第二个链式方法调用中返回您的值。
Mockito的作者注意到,这 仅应用于遗留代码
。否则,更好的方法是将行为推送到CacheContext并提供完成工作本身所需的任何信息。您从CacheContext中获取的信息量表明您的类具有令人羡慕的功能。



