gpt4 book ai didi

java - 使用匹配器的mockito的InvalidUseOfMatchersException

转载 作者:行者123 更新时间:2023-12-02 00:46:01 30 4
gpt4 key购买 nike

我正在使用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"));

这是我的测试代码:

    @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());
}

这是 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()));
}

这些方法被称为:

        private boolean validateShipRoomExist() {
return roomService.existShipRoomDefault();
}

public boolean validateKidAlreadyRegistered(@NotNull String kidId) {
return kidRepository.existsById(kidId);
}

接下来是我在 roomService 中的代码:

public boolean existShipRoomDefault() {
return roomRepository.existsById(DEFAULT_AGGREGATOR_ID);
}

问题出在这个有问题的方法中,即使我在这个测试中使用了字符串anyString()。我不明白在这种情况下发生了什么。一个有趣的想法是,在 Debug模式下,如果我有断点,测试不会失败。

最佳答案

BDDMockito.startsWith 是一个 Matcher 方法,您在与模拟无关的 expectedException.expectMessage 上使用它。

如果您想评估异常消息,您应该将其与完整消息进行比较。

调整后的测试可能如下所示:

@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);
}

由于我不知道 MockDTO 类在做什么,并且我认为自己创建 KidDTO 实例很容易,所以我只是在上面的示例中这样做。

另一种选择是 - 如果 kidDTO 是一个模拟 - 定义:

BDDMockito.given(kidDto.getId()).willReturn("1");

关于java - 使用匹配器的mockito的InvalidUseOfMatchersException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57897298/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com