Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mockito not allowing Matchers.any() with Integer.class

I am trying to unit test this method:

/**
     * finds all widget descriptions containing specified text
     * @param searchText
     * @return
     */
    @Transactional
    public List<Integer> returnWidgetIdsFromSearchWord(String searchText){
        List<Integer> widgetIds = new ArrayList<Integer>();
        MapSqlParameterSource args = new MapSqlParameterSource();

        try{
            widgetIds = (List<Integer>) jdbt.queryForList("SELECT idwidgets FROM descriptions "
                    + "WHERE descriptiontext LIKE '%"+ searchText + "%'", args, Integer.class);
        }catch(Exception e){

        }

        return widgetIds;
    }

with this JUnit test:

@Test
    public void testReturnWidgetIdsFromSearchWord(){
        List<Integer> widgetIds = null;

        when(jdbt.queryForList(Matchers.anyString(), 
                Matchers.any(MapSqlParameterSource.class),
                 Matchers.any(Integer.class))).thenReturn(idList);

        widgetIds = (List<Integer>) dDao.returnWidgetIdsFromSearchWord("someText");

        assertEquals(widgetIds, idList);
    }

I have tried just use Integer.class without the Matcher - no luck because then it complains about needing 3 matchers. Any suggestions? And thanks

like image 711
eggshell Avatar asked Dec 26 '22 14:12

eggshell


2 Answers

Do not cast Matchers.anyVararg(), there is better solution.

Method queryForList has signature

queryForList(String sql, SqlParameterSource paramSource, Class<T> elementType)

so instead of

when(jdbt.queryForList(Matchers.anyString(), 
                       Matchers.any(MapSqlParameterSource.class),
                       Matchers.any(Integer.class))).thenReturn(idList); 

use

when(jdbt.queryForList(Matchers.anyString(), 
                       Matchers.any(MapSqlParameterSource.class), 
                       Matchers.<Class<Integer>>any())).thenReturn(idList);

as described in Mockito: Verifying with generic parameters


Do not use code with anyVararg() and casting

when(jdbt.queryForList(Matchers.anyString(), 
                       Matchers.any(MapSqlParameterSource.class), 
                       (Class<Object>) Matchers.anyVararg()).thenReturn(idList);

because this generate warning

Unchecked cast: `java.lang.Object` to `java.lang.Class<java.lang.Object>`
like image 127
MariuszS Avatar answered Jan 07 '23 19:01

MariuszS


If you need to mock NamedParameterJdbcTemplate#queryForList(String, SqlParameterSource, Class) then just use

when(jdbt.queryForList(Matchers.anyString(), Matchers.any(SqlParameterSource.class), Matchers.any(Class.class))).thenReturn(idList);

Is it possible that you didn't pass your template object to the DAO instance? Find my full test class below. It passes the test successfully:

import static org.junit.Assert.*;
import static org.mockito.Matchers.*;
import static org.mockito.Mockito.*;

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

import org.junit.Test;
import org.mockito.Mockito;

public class DebugTest {

    private MyDao dDao;

    private final NamedParameterJdbcTemplate jdbt = mock(NamedParameterJdbcTemplate.class);

    @SuppressWarnings("unchecked")
    @Test
    public void testReturnWidgetIdsFromSearchWord() {
        final List<Integer> idList = new ArrayList<Integer>();

        this.dDao = new MyDao(this.jdbt);

        when(this.jdbt.queryForList(anyString(), any(SqlParameterSource.class), any(Class.class)))
            .thenReturn(idList);

        final List<Integer> widgetIds = this.dDao.returnWidgetIdsFromSearchWord("Hallo");

        assertEquals(widgetIds, idList);

    }

    private static class MyDao {
        private final NamedParameterJdbcTemplate jdbt;

        public MyDao(final NamedParameterJdbcTemplate jdbt) {
            this.jdbt = jdbt;
        }

        public List<Integer> returnWidgetIdsFromSearchWord(final String searchText) {
            List<Integer> widgetIds = new ArrayList<Integer>();
            SqlParameterSource args = new MapSqlParameterSource();

            try {
                widgetIds = (List<Integer>) jdbt.queryForList("SELECT idwidgets FROM descriptions "
                    + "WHERE descriptiontext LIKE '%"+ searchText + "%'", args, Integer.class);
            } catch(Exception e) {

            }

            return widgetIds;
        }
    }
}
like image 40
Peter Keller Avatar answered Jan 07 '23 18:01

Peter Keller