【发布时间】:2021-10-18 00:11:24
【问题描述】:
我正在为作为行为传递的方法创建测试,我不确定模拟如何用于功能性。我不想模拟 executeBehaviour 调用,而是模拟行为函数的实际执行。应用
public String processData(){
String a="check";
return executeBehaviour((check)->"hello"+check,a);
}
public String executeBehaviour(Function<String,String> data,String data1){
//Some processing
return data.apply(data1);
}
我已经编写了以下测试用例,但它似乎没有模拟 data.apply() 称呼。 测试用例:
@Test
void sampleTest() {
Function<String, String> processFunction = mock(Function.class);
String test = "check";
when(groupingFunction.apply(anyString())).thenReturn(test);
String data = itemInventoryProcessorService.executeBehaviour(processFunction,test);
Assertions.assertEquals("check", data);
}
断言失败,因为写入的数据是实际执行的行为,即“hellocheck”而不是模拟的“check”。
【问题讨论】:
标签: unit-testing mockito spring-boot-test