Mockito - 0 Matchers Expected, 2 Recorded (InvalidUseOfMatchersException)

Vidhi :

I referred to all available resources on Stackoverflow for similar queries. But I'm not sure what the issue with this test is:

It is throwing the following exception.

[main] ERROR com.example.dao.spring.TransactionDAOSpring - org.mockito.exceptions.misusing.InvalidUseOfMatchersException: Invalid use of argument matchers!0 matchers expected, 2 recorded.

Following is the code:

import static org.junit.Assert.assertEquals;
import static org.mockito.Matchers.anyLong;
import static org.mockito.Matchers.anyMapOf;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.when;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.sql.DataSource;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.simple.SimpleJdbcCall;

import com.example.dto.DisplayTwo;
import com.example.dto.DisplayOne;
import com.example.dto.DisplayThree;

public class TransactionDAOSpringTest {

    TransactionDAOSpring transactionDAOSpring;

    @Mock
    DataSource dataSource;
    @Mock
    JdbcTemplate jdbcTemplate;
    @Mock
    SimpleJdbcCall storedProc;

    @Rule
    public ExpectedException thrown = ExpectedException.none();

    private Map<String, Object> resultMap;
    private List<DisplayOne> displayOne;
    private List<DisplayTwo> displayTwo;
    private List<DisplayThree> displayThree;

    @Before
    public void beforeMethod() {
        MockitoAnnotations.initMocks(this);
        transactionDAOSpring = new TransactionDAOSpring();
        transactionDAOSpring.setJdbcTemplate(jdbcTemplate);
        transactionDAOSpring.setDataSource(dataSource);
        transactionDAOSpring.retrieveResultStoredProc = storedProc;
        resultMap = new HashMap<String, Object>();
        displayOne = new ArrayList<DisplayOne>();
        displayTwo = new ArrayList<DisplayTwo>();
        displayThree = new ArrayList<DisplayThree>();
    }

    @Test
    public void testRetrieve_When_ResultSet_Not_Empty() {
        displayOne.add(new DisplayOne());
        displayTwo.add(new DisplayTwo());
        displayThree.add(new DisplayThree());

        resultMap.put("DisplayOneResultSet", displayOne);
        resultMap.put("DisplayTwoResultSet", displayTwo);
        resultMap.put("DisplayThreeResultSet", displayThree);

        when(storedProc.execute(anyMapOf(String.class, Object.class)))
                .thenReturn(resultMap);
        Map<String, Object> returnedResultMap = transactionDAOSpring.retrieve(anyString(),
                anyLong());
        assertEquals(resultMap.size(), returnedResultMap.size());
    }

Update: After debugging, it looks like it fails to getConnection from the dataSource and hence throws the exception.

Any help would be appreciated.

gokareless :

Matchers like anyString() or anyLong() can be used for mocking an object, for example inside when() or verify() invocation. In your case:

 Map<String, Object> returnedResultMap = transactionDAOSpring.retrieve(
                                                           anyString(), anyLong());

is real method call. I think that is what causes InvalidUseOfMatchersException. Try invoking your method with stub values like empty string and 0L

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=148054&siteId=1