[英]InvalidUseOfMatchersException with mockito using matchers
I am working with mockito and I am getting the next issue: 我正在使用Mockito,并且遇到了下一个问题:
java.lang.AssertionError:
Expected: (an instance of java.lang.IllegalArgumentException and exception with message a string containing "")
but: an instance of java.lang.IllegalArgumentException <org.mockito.exceptions.misusing.InvalidUseOfMatchersException:
Invalid use of argument matchers!
0 matchers expected, 1 recorded:
-> at com.rccl.middleware.kidsclub.engine.services.KidServiceTest.saveKid_kidExist_throwException(KidServiceTest.java:97)
This exception may occur if matchers are combined with raw values:
//incorrect:
someMethod(anyObject(), "raw String");
When using matchers, all arguments have to be provided by matchers.
For example:
//correct:
someMethod(anyObject(), eq("String by matcher"));
This is my test code: 这是我的测试代码:
@Mock
private KidRepository kidRepository;
@Mock
private RoomService roomService;
@Test
public void saveKid_kidExist_throwException() {
given(kidRepository.existsById(anyString())).willReturn(true);
given(roomService.existShipRoomDefault()).willReturn(true);
expectedException.expect(IllegalArgumentException.class);
expectedException.expectMessage(startsWith("Kid already registered"));
KidDTO kidDTO = MockDTO.buildKidDTO();
service.saveKid(kidDTO);
then(kidRepository).should().existsById(anyString());
}
This is the code of the saveKid method which is sending exceptions basically: 这是saveKid方法的代码,该方法基本上发送异常:
if (!validateShipRoomExist()) {
log.warn("::: The ShipRoom document doesn't exist.");
throw new RoomNotFoundException(NO_ROOMS_IN_DATABASE);
}
if (validateKidAlreadyRegistered(kidDTO.getId())) {
log.warn("::: Trying to persist a Kid already persisted with ID [{}]", kidDTO.getId());
throw new IllegalArgumentException(String.format("Kid already registered with ID [%s]", kidDTO.getId()));
}
And these are the methods called: 这些方法称为:
private boolean validateShipRoomExist() {
return roomService.existShipRoomDefault();
}
public boolean validateKidAlreadyRegistered(@NotNull String kidId) {
return kidRepository.existsById(kidId);
}
The next is my code in the roomService: 接下来是我在roomService中的代码:
public boolean existShipRoomDefault() {
return roomRepository.existsById(DEFAULT_AGGREGATOR_ID);
}
The problem is in this method which has the problem, even though I am using a string anyString() in this test. 问题在于此方法存在问题,即使我在此测试中使用字符串anyString()也不例外。 I don't understand what is happening in this case. 我不知道在这种情况下发生了什么。 an interesting think is that in debug mode the test doesn't fails if I have a breakpoint. 一个有趣的想法是,在调试模式下,如果有断点,测试不会失败。
BDDMockito.startsWith
is a Matcher method, which you used on expectedException.expectMessage
which is unrelated to mocking. BDDMockito.startsWith
是一个匹配器方法,你所使用的expectedException.expectMessage
这是无关的嘲讽。
If you want to evaluate the message of the exception you should compare it to the complete message. 如果要评估异常消息,则应将其与完整消息进行比较。
An adjusted test could look like this: 经过调整的测试可能如下所示:
@Test
public void saveKid_kidExist_throwException() {
// ...
KidDTO kidDto = new KidDTO();
kidDto.setId("1");
expectedException.expect(IllegalArgumentException.class);
expectedException.expectMessage("Kid already registered with ID [1]");
service.saveKid(kidDto);
}
Since I don't know what the MockDTO class is doing and I assume its easy to create an instance of KidDTO
on your own, I just do that in the example above. 因为我不知道MockDTO类在做什么,并且我认为可以很容易地自己创建KidDTO
实例, KidDTO
我只是在上面的示例中这样做。
An alternative would be - in case kidDTO
is a mock - to define: 一个替代方案是-如果kidDTO
是模拟的-定义:
BDDMockito.given(kidDto.getId()).willReturn("1");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.