Skip to content
Advertisement

Invalid use of argument matchers! 2 matchers expected, 1 recorded

I am writing a test to validate creations of trolleys. So,it is just a service method which basically creates number of trolleys in the database. I am mocking the repository using mockito.

So, What i am doing is I am mocking and save and getAll functionality of Repository.

Here is what my code looks like:-

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

}

So initially i am just taking an empty array list and mocking the save method to add to the list. Because service method calls the save method which takes a trolley and save it to db for each trolley it creates and it returns Page Of Trolley which calls the findAllByStoreHouseCode which takes storehousecode and a PageRequest.

Error occurs on the line where i am using the any ArgumentMatcher. I am new to testing could any one please tell me where i am wrong or this approach is wrong and i should implement test in some other way.

Here is the error:-

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

Advertisement

Answer

Try changing your Mockito setup to below

Mockito.when(trolleyRepository.findAllByStoreHouseCode(Mockito.eq(SH_CODE), Mockito.any(PageRequest.class))).thenReturn(getTrolleyList());
Mockito.when(trolleyRepository.save(Mockito.any(Trolley.class))).thenAnswer(answer);
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement