Mockito不允许Matchers.any()使用Integer.class

时间:2014-01-29 20:09:06

标签: java junit mockito junit4 matcher

我正在尝试对此方法进行单元测试:

/**
     * 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;
    }

使用此JUnit测试:

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

我试过在没有匹配器的情况下使用Integer.class - 没有运气,因为它抱怨需要3个匹配器。有什么建议?谢谢

3 个答案:

答案 0 :(得分:8)

不要投射Matchers.anyVararg(),有更好的解决方案。

方法queryForList有签名

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

所以而不是

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

使用

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

Mockito: Verifying with generic parameters

中所述

不要将代码用于anyVararg()并投射

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

因为这会生成警告

Unchecked cast: `java.lang.Object` to `java.lang.Class<java.lang.Object>`

答案 1 :(得分:1)

如果您需要模拟NamedParameterJdbcTemplate#queryForList(String, SqlParameterSource, Class),请使用

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

您是否可能未将模板对象传递给DAO实例?在下面找到我的完整测试课程。它成功通过了测试:

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

答案 2 :(得分:-1)

我将测试改为此测试并且有效:

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

        /*when(jdbt.queryForList("SELECT idwidgets FROM descriptions "
                + "WHERE descriptiontext LIKE '%"+ searchText + "%'", 
                args, Integer.class)).thenReturn(idList);*/

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

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

        System.out.println(widgetIds.size());

        assertEquals(widgetIds, idList);
    }

感谢您的帮助