我有一个 spring shell 应用程序。我需要测试命令。我的命令:
@Autowired
private RemoteService remoteService;
@ShellMethod
public String list(){
List<String> items= remoteService.getAll();
return items.toString();
}
我的测试:
@Test
public void listCommandTest(){
RemoteService remoteService=mock(RemoteService.class);
when(remoteService.getAll()).thenReturn(new ArrayList<>());
shell.evaluate(()->"list");
verify(remoteConfigService).getAll();
}
我不需要调用 RemoteService 的真正方法 getAll(),但它被调用了。如何解决?
最佳答案
您正在模拟 when(remoteService.getAll(anyString()))
方法,并且正在调用 getAll()
。
将 when(remoteService.getAll(anyString()))
替换为 when(remoteService.getAll())
关于java - 如何使用 Mockito 模拟服务?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50059638/