How to mock JdbcTemplate.queryForObject() method

buttowski picture buttowski · Dec 11, 2012 · Viewed 23.4k times · Source

My method looks like this:

public class Decompile extends JdbcDaoSupport
public void getRunner(){
String val = this.getJdbcTemplate().queryForObject(sql,String.class, new Object[]{1001});
}
}

Please suggest how I would mock this.

Answer

buttowski picture buttowski · Dec 11, 2012
@Mock
JdbcTemplate jdbctemplate;

@Test
public void testRun(){
when(jdbctemplate.queryForObject(anyString(),eq(String.class),anyObject()).thenReturn("data");
}