using JMock to write unit test for a simple spring JDBC DAO
Posted
by
Quincy
on Stack Overflow
See other posts from Stack Overflow
or by Quincy
Published on 2012-04-12T16:55:48Z
Indexed on
2012/04/12
17:29 UTC
Read the original article
Hit count: 431
I'm writing an unit test for spring jdbc dao. The method to test is:
public long getALong() {
return simpleJdbcTemplate.queryForObject("sql query here", new RowMapper<Long>() {
public Long mapRow(ResultSet resultSet, int i) throws SQLException {
return resultSet.getLong("a_long");
}
});
}
Here is what I have in the test:
public void testGetALong() throws Exception {
final Long result = 1000L;
context.checking(new Expectations() {{
oneOf(simpleJdbcTemplate).queryForObject("sql_query", new RowMapper<Long>() {
public Long mapRow(ResultSet resultSet, int i) throws SQLException {
return resultSet.getLong("a_long");
}
});
will(returnValue(result));
}});
Long seq = dao.getALong();
context.assertIsSatisfied();
assertEquals(seq, result);
}
Naturally, the test doesn't work (otherwise, I wouldn't be asking this question here). The problem is the rowmapper in the test is different from the rowmapper in the DAO. So the expectation is not met.
I tried to put with
around the sql query and with(any(RowMapper.class))
for the rowmapper. It wouldn't work either, complains about "not all parameters were given explicit matchers: either all parameters must be specified by matchers or all must be specified by values, you cannot mix matchers and values"
© Stack Overflow or respective owner