【问题标题】:Mockito not allowing Matchers.any() with Integer.classMockito 不允许带有 Integer.class 的 Matchers.any()
【发布时间】:2014-01-29 20:09:06
【问题描述】:

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

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

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

【问题讨论】:

标签: java junit mockito junit4 matcher


【解决方案1】:

不要投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>`

【讨论】:

  • 这个我也试过了,也没有通过。此外,我从未收到“未经检查的演员表”警告。不过谢谢
【解决方案2】:

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

【讨论】:

  • 我添加了完整的测试课程。我希望它有所帮助。
【解决方案3】:

我把我的测试改成了这个,它成功了:

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

感谢您的帮助

【讨论】:

  • 拜托,拜托,拜托,在现实生活中,为 Mockito 静态方法使用静态导入,否则使用静态匹配器获取 when 子句的简单性会变得非常困难方法调用
猜你喜欢
  • 2023-03-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-06-22
相关资源
最近更新 更多