簡體   English   中英

參數匹配器的無效使用! 預計有 2 名匹配者,已記錄 1 名

[英]Invalid use of argument matchers! 2 matchers expected, 1 recorded

我正在編寫一個測試來驗證手推車的創作。 所以,它只是一種服務方法,它基本上在數據庫中創建了手推車的數量。 我正在使用 mockito 模擬存儲庫。

所以,我正在做的是模擬、保存和獲取存儲庫的所有功能。

這是我的代碼的樣子:-

package com.service;

import com.model.Bin;
import com.model.Trolley;
import com.model.dao.NewTrolleyDao;
import com.model.enums.BinType;
import com.model.enums.TrolleyType;

import com.repository.TrolleyRepository;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.ArrayList;
import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;

@RunWith(SpringRunner.class)
@SpringBootTest
public class TrolleyServiceTests {

    @MockBean
    private TrolleyRepository trolleyRepository;

    private TrolleyService trolleyService;

    private static final long SH_CODE = 123;
    private static final TrolleyType trolleyType = TrolleyType.QS;
    private List<Trolley> trolleyList = new ArrayList<>();

    @Before
    public void setUp() {
        Answer<Trolley> answer = new Answer<Trolley>() {
            public Trolley answer(InvocationOnMock invocation) {
                Trolley trolley = invocation.getArgument(0, Trolley.class);
                trolleyList.add(trolley);
                System.out.println(trolleyList.size());
                return trolley;
            }
        };
        Mockito.when(trolleyRepository.findAllByStoreHouseCode(SH_CODE, any(PageRequest.class))).
                thenReturn(getTrolleyList());
        Mockito.when(trolleyRepository.save(any(Trolley.class))).
                thenAnswer(answer);
        trolleyService = new TrolleyService(trolleyRepository);
    }
    private Page<Trolley> getTrolleyList() {
        Page<Trolley> trolleys = new PageImpl<Trolley>(trolleyList);
        return  trolleys;
    }
    private NewTrolleyDao getNewTrolleyDao(int size) {
        NewTrolleyDao newTrolleyDao = new NewTrolleyDao();
        newTrolleyDao.setStoreHouseCode(SH_CODE);
        newTrolleyDao.setType(trolleyType);
        newTrolleyDao.setSize(size);
        return newTrolleyDao;
    }

    @Test
    public void testCreateTrolleys() {
        NewTrolleyDao trolleyDao = getNewTrolleyDao(30);
        Page<Trolley> res = trolleyService.createTrolleys(trolleyDao);
        assertThat(res.getTotalElements()).isEqualTo(30);
    }

}

所以最初我只是取一個空數組列表並模擬保存方法以添加到列表中。 因為 service 方法調用 save 方法,該方法獲取一個手推車並將其保存到它創建的每個手推車的數據庫中,並返回 Page Of Trolley ,它調用 findAllByStoreHouseCode ,它接受 storehousecode 和一個 PageRequest。

錯誤發生在我使用任何ArgumentMatcher 的行上。 我是測試新手,請任何人告訴我我哪里錯了,或者這種方法是錯誤的,我應該以其他方式實施測試。

這是錯誤:-

-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
Test set: com.service.TrolleyServiceTests
-------------------------------------------------------------------------------
Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 2.948 s <<< FAILURE! - in com.service.TrolleyServiceTests
testCreateTrolleys  Time elapsed: 0.037 s  <<< ERROR!
org.mockito.exceptions.misusing.InvalidUseOfMatchersException: 

Invalid use of argument matchers!
2 matchers expected, 1 recorded:
-> at com.service.TrolleyServiceTests.setUp(TrolleyServiceTests.java:52)

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.

    at com.service.TrolleyServiceTests.setUp(TrolleyServiceTests.java:52)

嘗試將您的Mockito設置更改為以下

Mockito.when(trolleyRepository.findAllByStoreHouseCode(Mockito.eq(SH_CODE), Mockito.any(PageRequest.class))).thenReturn(getTrolleyList());
Mockito.when(trolleyRepository.save(Mockito.any(Trolley.class))).thenAnswer(answer);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM