【发布时间】:2020-05-21 17:59:59
【问题描述】:
我正在尝试模拟来自受保护方法的响应,但出现此错误:
参数匹配器的使用无效! 预计匹配 1 个,记录 2 个:
这是代码:
@Test
public void updateClientSettings() {
String clientId = "36000150-730a-4808-b04b-2b264862de8a";
ClientSettings updateSettings = new ClientSettings();
updateSettings.setHeadline("Any headline");
updateSettings.setSecondaryLine("Any secondary line");
byte[] content = { 0x47, 0x49, 0x46, 0x38, 0x39, 0x61, 0x1, 0x0, 0x1, 0x0, 0x8, 0x0, 0x0, 0x8, 0x8, 0x8, 0x0, 0x0, 0x0, 0x2c, 0x0, 0x0, 0x0, 0x0, 0x1, 0x0, 0x1, 0x0, 0x0, 0x2, 0x2, 0x44, 0x1, 0x0, 0x3b };
MultipartFile backgroundImage = new MockMultipartFile("image_test.png", "image_test.png", "image/png", content);
try {
ClientSettings settings = new ClientSettings();
settings.setClientId(clientId);
when(clientSettingsRepository.findByClientId(clientId)).thenReturn(settings);
boolean validate = true;
when(clientService.uploadClientBackGroundImage(Mockito.anyString(), any(MultipartFile.class))).thenReturn(validate);
ServiceResponse response = clientService.updateSettings(clientId, updateSettings, backgroundImage);
assertNotNull(response);
} catch (Exception e) {
fail();
}
}
我的所有项目中只有 1 个 uploadClientBackGroundImage 方法,并接收 2 个参数作为输入
protected boolean uploadClientBackGroundImage(String clientId, MultipartFile backgroundImage) { ...
任何建议将不胜感激。
【问题讨论】:
-
你的 clientService 是模拟对象还是间谍或存根?
-
是一个 InjectedMock
-
请注意 - 您的测试中的
try和catch是不必要的,这会使您的操作更加困难。如果测试抛出异常而没有捕获它,则测试框架将报告该异常并且测试将失败。这就是你想要发生的事情。使用您编写的代码,异常的详细信息将被丢弃,这使得修复变得更加困难。
标签: java spring-boot mockito matcher argument-matcher