【发布时间】:2017-12-14 21:45:41
【问题描述】:
我一直无法找到一种方法来使用“深度存根”对 Mockito 中的间谍进行存根方法。我想做的是这样的:
@Spy private Person person = //retrieve person
@Test
public void testStubbed() {
doReturn("Neil").when(person).getName().getFirstName();
assertEquals("Neil", person.getName().getFirstName());
}
上面的代码编译没有问题,但是在运行测试时,它失败了,说 getName() 不能返回返回类型(在这种情况下是 Name 类)。
通常,在模拟时,您必须为每个模拟对象使用@Mock(answer = Answers.RETURNS_DEEP_STUBS)。但是,spy 似乎没有这样的东西。
有没有人使用间谍成功地进行过深度模拟?
我收到的错误如下:
String cannot be returned by getName()
getName() should return Name
Due to the nature of the syntax above problem might occur because of:
1. Multithreaded testing
//I'm not doing multithreaded testing
2. A spy is stubbed using when(spy.foo()).then() syntax. It is safer to stub spies with doReturn|Throw() family of methods
//As shown above, I'm already using the doReturn family of methods.
【问题讨论】: