How to mock JdbcTemplate.queryForObject() method
@Mock
JdbcTemplate jdbctemplate;
@Test
public void testRun(){
when(jdbctemplate.queryForObject(anyString(),eq(String.class),anyObject()).thenReturn("data");
}
an EasyMock-3.0 example
String sql = "select * from t1";
Object[] params = new Object[] { 1001 };
JdbcTemplate t = EasyMock.createMock(JdbcTemplate.class);
EasyMock.expect(
t.queryForObject(sql, String.class, params)).andReturn("res");
EasyMock.replay(t);
Use JMockit, the code will be like this:
@Mocked
JdbcTemplate jdbcTemplate
new Expectations() {
jdbcTemplate.queryForObject(sql,String.class, new Object[]{1001});
result = "result you want";
}
More information about JMockit is here.