问题是你的存根
State
对象不知道
getValue()
应该是传递给的最后一个值
setValue()
。
Mockito创建的存根不会自动识别出您有一个getter和setter,getter应该返回setter上次调用的内容。它只是一个愚蠢的存根,只做你告诉它做的事,而你没有告诉它什么时候做
setValue()
被调用。
最简单的方法是验证
setValue
使用舍入值调用,而不是尝试从
getValue()
:
Mockito.verify(context.getState()).setValue(AdditionalMatchers.eq(4.123, 0.0));
另一种方法是创建测试
状态
类,并在测试中使用它。当然,能否做到这一点取决于这个类有多少其他方法。如果你要写这样一个类(我把它命名为
TestState
在下面的代码中),测试代码如下所示:
@Test
public void testProcess2(){
Context context = Mockito.mock(Context.class);
Mockito.when(context.getState()).thenReturn(new TestState());
context.getState().setValue(4.12345);
sample.process(context);
assertEquals(4.123,context.getState().getValue(),0.0);
}