Mockito不允许使用Integer.Class的Matcher.any()



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

/**
     * 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个匹配者。有什么建议么?谢谢

不要施放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中所述:使用通用参数验证


请勿将代码与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>`

如果您需要模拟名为Parameterjdbctemplate#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;
        }
    }
}

我将测试更改为此,它奏效了:

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

感谢您的帮助

相关内容

  • 没有找到相关文章