尝试进行代码测试时,为什么会不断收到“无效使用参数匹配器”的信息?

问题描述

我确实找到了很多与此主题有关的问题,但没有一个答案似乎对我的情况有所帮助。问题在于抛出错误代码与运行良好的代码几乎相同。

变量:

@BeforeEach
void setUp() {
    MockitoAnnotations.initMocks(this);

    Product productTest = new Product(1,name,225,1.5,4.5,"picture",46,1,orderProductList);
    WarehouSEOrder warehouSEOrder = new WarehouSEOrder(1);

    Optional<Product> optProductTest = Optional.of(productTest);
    Optional<WarehouSEOrder> optOrdersTest = Optional.of(warehouSEOrder);

    List<Integer> listofIds = new ArrayList<>();
    listofIds.add(anyInt());
}

这是有效的测试:

@Test
    void getordersByUser() {

        List<Orders> ordersList = new ArrayList<>();
        ordersList.add(orders);

        OrderProduct orderProductTest = new OrderProduct(productTest,orders,55);
        List<OrderProduct> orderProducts = new ArrayList<>();
        orderProducts.add(orderProductTest);

        when(orderRepository.findByUserId(anyInt())).thenReturn(ordersList);
        when(orderProductRepository.findByOrderId(anyInt())).thenReturn(orderProducts);
        when(orderRepository.findById(anyInt())).thenReturn(optOrdersTest);
        when(productRepository.findById(anyInt())).thenReturn(optProductTest);

        List<OrderVO> orderVOS = orderService.getordersByUserId(1);
        OrderVO orderVO = orderVOS.get(0);

        assertEquals(orderVO.getUserId(),orders.getUserId());
    }

然后测试显示错误

@Test
    void getordersByUser() {

        List<WarehouSEOrder> warehouSEOrderList = new ArrayList<>();
        warehouSEOrderList.add(warehouSEOrder);

        OrderProduct orderProductTest = new OrderProduct(productTest,warehouSEOrder,55);
        List<OrderProduct> orderProducts = new ArrayList<>();
        orderProducts.add(orderProductTest);


        when(orderRepository.findByUserId(anyInt())).thenReturn(warehouSEOrderList);
        when(orderRepository.findById(anyInt())).thenReturn(optOrdersTest);
        when(productRepository.findById(anyInt())).thenReturn(optProductTest);
        when(orderProductRepository.findByWarehouSEOrderIdIn(listofIds)).thenReturn(orderProducts);

        List<OrderVO> orderVOS = orderService.getordersByUserId(1);

        assertEquals(orderVOS.get(0).getUserId(),warehouSEOrder.getUserId());
    }

因此,基本上唯一真正的区别是,它不使用:

when(orderProductRepository.findByOrderId(anyInt())).thenReturn(orderProducts);

我使用:

when(orderProductRepository.findByWarehouSEOrderIdIn(listofIds)).thenReturn(orderProducts);

之所以这样做是因为我对实际代码进行了更改,所以我不从foreach循环中调用findByOrderId来获取所有ID,而是只对databse进行一次调用:findByWarehouSEOrderIdIn。 无论如何,结果是相同的,代码本身也可以正常工作,但是我似乎也无法使测试正常工作。

如果我将项目添加listofIds中,例如:

 listofIds.add(anyInt()); 

我得到一个错误

org.mockito.exceptions.misusing.InvalidUSEOfMatchersException: 
Invalid use of argument matchers!
0 matchers expected,1 recorded:
-> at com.example.repositoryservice.serviceImplementations.OrderServiceImpltest.setUp(OrderServiceImpltest.java:70)

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

“ OrderServiceImpltest.java:70”指向listofIds.add(anyInt());

否则,如果我添加原始值而不是像这样的匹配器:

listofIds.add(1); 

我得到一个错误

Array index out of range: 0

指向行“ assertEquals(orderVOS.get(0).getUserId(),WarehouSEOrder.getUserId());”因此基本上,即使代码可以正常工作,它也无法按预期工作。

由于这两个代码几乎相同,但结果不同,因此似乎没有错误

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)