繁体   English   中英

InvalidUseOfMatchersException与使用匹配器的模仿

[英]InvalidUseOfMatchersException with mockito using matchers

我正在使用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()也不例外。 我不知道在这种情况下发生了什么。 一个有趣的想法是,在调试模式下,如果有断点,测试不会失败。

BDDMockito.startsWith是一个匹配器方法,你所使用的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我只是在上面的示例中这样做。

一个替代方案是-如果kidDTO是模拟的-定义:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM