我正在测试JdbcSpitterDao#getSpitterByid()
并且正在嘲笑jdbcTemplate.queryForObject()
public class JdbcSpitterDao extends JdbcDaoSupport implements SpitterDao {
...
public Spitter getSpitterById(long id,
ParameterizedRowMapper<Spitter> parameterizedRowMapper) {
JdbcTemplate jdbcTemplate = getJdbcTemplate();
return jdbcTemplate.queryForObject(SQL_SELECT_SPITTER,
parameterizedRowMapper, id);
}
...
}
我的测试对象包含以下内容
ParameterizedRowMapper<Spitter> parameterizedRowMapper = new ParameterizedRowMapper<Spitter>() {
public Spitter mapRow(ResultSet rs, int rowNum) throws SQLException {
Spitter Spitter = new Spitter();
spitter.setId(rs.getLong(1));
spitter.setUsername(rs.getString(2));
spitter.setPassword(rs.getString(3));
spitter.setFullName(rs.getString(4));
spitter.setEmail(rs.getString(5));
return spitter;
}
};
JdbcTemplate jdbcTemplate = mock(JdbcTemplate.class);
JdbcSpitterDao jdbcSpitterDao = new JdbcSpitterDao();
jdbcSpitterDao.setJdbcTemplate(jdbcTemplate);
// Don't connect to the database. Mock JdbcTemplate class
when(
jdbcTemplate.queryForObject(
JdbcSpitterDao.SQL_SELECT_SPITTER,
parameterizedRowMapper, 1)).thenReturn(new Spitter(1, "rajkumarm", "rajmukarm",
"Rajkumar Masaniayan", "rajkumarm@gmail.com"));
// Actual test
Spitter actualSpitter = jdbcSpitterDao.getSpitterById(1, parameterizedRowMapper);
但when().thenReturn()
成语不会被调用。控件正在流向实际的jdbcTemplate
。看起来有问题
JdbcTemplate jdbcTemplate = mock(JdbcTemplate.class);
或
jdbcSpitterDao.setJdbcTemplate(jdbcTemplate);
请您告诉我出了什么问题?
答案 0 :(得分:0)
有这样的建议不要嘲笑你不拥有的类型。一般来说这是一个很好的建议,在这里真的有意义的是不要单独测试dao。为什么不只是集成测试sth,它在DB上运行而没有额外的逻辑呢?