小编egg*_*ell的帖子

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

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

/**
     * 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;
    }
Run Code Online (Sandbox Code Playgroud)

使用此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);
    }
Run Code Online (Sandbox Code Playgroud)

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

java junit matcher junit4 mockito

3
推荐指数
1
解决办法
2万
查看次数

标签 统计

java ×1

junit ×1

junit4 ×1

matcher ×1

mockito ×1