【问题标题】:Mockito InvalidUseOfMatchersException when I send matchers as Parameters当我将匹配器作为参数发送时,Mockito InvalidUseOfMatchersException
【发布时间】:2019-09-11 04:14:43
【问题描述】:

我正在使用 mockito,但遇到了与匹配器参数相关的问题。

我遇到了这个异常:

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: 
Invalid use of argument matchers!
2 matchers expected, 1 recorded:
-> at com.rccl.middleware.kidsclub.engine.services.RoomServiceTest.findBetweenMinAgeAndMaxAge_roomNot(RoomServiceTest.java:43)

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"));

For more info see javadoc for Matchers class.

尽管我在所有参数中都使用了匹配器,但我收到了此错误消息,我想这是因为我在 roomService.findByAge 中发送了一个匹配器,但此方法正在调用第二个匹配器 findBetweenMinAgeAndMaxAge 并且内部有下一个调用中的另外两个参数。我不太确定问题的原因以及如何解决它。

这是我的测试:

import com.rccl.middleware.kidsclub.engine.mock.MockDTO;
import com.rccl.middleware.kidsclub.engine.repository.RoomRepository;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import java.util.Optional;
import com.rccl.middleware.kidsclub.engine.repository.model.ShipRoom.Room;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.BDDMockito.given;


@RunWith(MockitoJUnitRunner.class)
public class RoomServiceTest {

    @Mock
    private RoomRepository roomRepository;
    @Rule
    public final ExpectedException expectedException = ExpectedException.none();

    private RoomService roomService;

    @Before
    public void init() {
        this.roomService = new RoomService(roomRepository);
    }

    @Test()
    public void findBetweenMinAgeAndMaxAge_roomNot() {
        int numRoomDTO = 3;
        Optional<Room> room = Optional.ofNullable(MockDTO.buildRandomRoom(numRoomDTO));
        given(roomRepository.findBetweenMinAgeAndMaxAge(any(Integer.class), anyString())).willReturn(room);
        roomService.findByAge(any(Integer.class));
    }
}

这是 findByAge 方法:

public Optional<RoomDTO> findByAge(int childAge) {
        return roomRepository.findBetweenMinAgeAndMaxAge(childAge, DEFAULT_AGGREGATOR_ID)
                .map(room -> ObjectMapperUtils.map(room, RoomDTO.class));
    }

是MockDTO类中的helper

public static Room buildRandomRoom(int index) {
        return new Room(RandomStringUtils.randomAlphabetic(10),
                RandomStringUtils.randomAlphabetic(10),
                RandomStringUtils.randomAlphabetic(15),
                RandomUtils.nextInt(0, 10),
                RandomUtils.nextInt(11, 20));
    }

【问题讨论】:

  • @daniu 的评论是正确的,这是我最初忽略的。我花了一些时间并添加了更多解释......并在其他地方补偿了他的贡献;-)

标签: java junit mockito matcher


【解决方案1】:

这里:

roomService.findByAge(any(Integer.class));

这是您的测试对生产代码的调用以“触发”某些活动,然后通过验证某些结果或某些模拟被按预期调用来检查这些活动。

但是:你不能(也不应该)在这里使用那个参数匹配器。走吧:

roomService.findByAge(42); // or whatever number makes sense there

换句话说:你提供了一个“真实”的论据。其中之一你“知道”应该发生什么。含义:您调用生产代码,并将特定的“上下文”传递给该调用。根据您的知识“当我通过 42 时,X 应该发生”,然后您验证“是的,X 确实发生了”。

参数匹配器仅在为 Mockito 模拟对象编写 规范 时使用。比如:参数匹配器匹配根据你的规范传入的参数,以做出某种决定。

参数匹配器并不神奇地知道如何创建一个值来传递到方法中。他们唯一的目的是匹配传入的参数反对某事。当然,他们的签名是以一种允许您编写代码的方式编写的。但这对于在您的模拟规范中方便地使用参数匹配器是必要的。匹配器并不真正返回值,它们只是接受它们。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多