### Question:
JdbcDaoTemplate { public <T> List<T> query(String sql, ColumnMapper<T> columnMapper) { return query(sql, null, columnMapper); } JdbcDaoTemplate(DataSource datasource); List<T> query(String sql, ColumnMapper<T> columnMapper); List<T> query(String sql, ParameterMapper parameterMapper, final ColumnMapper<T> columnMapper); void update(String sql, ParameterMapper parameterMapper); List<Number> insert(String sql, ParameterMapper parameterMapper); List<Number> update(String sql, final boolean returnKey, ParameterMapper parameterMapper); Object execute(String sql, boolean returnKey, ParameterMapper parameterMapper, PreparedCallback preparedCallback); DataSource getDatasource();  }### Answer:
@Test public void testQuery() throws SQLException { final PreparedStatement mockStatement = mock(PreparedStatement.class); when(mockConnection.prepareStatement("SQL")).thenReturn(mockStatement); final ResultSet mockResultSet = mock(ResultSet.class); when(mockStatement.executeQuery()).thenReturn(mockResultSet); when(mockResultSet.next()).thenReturn(true, true, false); when(mockResultSet.getString(anyInt())).thenReturn("string1", "string2"); List<String> rows = template.query("SQL", new ColumnMapper<String>() { @Override public String mapColumns(ResultSet resultset) throws SQLException { return resultset.getString(0); } }); assertEquals(2, rows.size()); assertThat(rows, hasItems("string1", "string2")); verify(mockResultSet).close(); verify(mockStatement).close(); verify(mockConnection).close(); }
@Test public void testQueryThrowException() throws SQLException { final PreparedStatement mockStatement = mock(PreparedStatement.class); when(mockConnection.prepareStatement("SQL")).thenReturn(mockStatement); final ResultSet mockResultSet = mock(ResultSet.class); when(mockStatement.executeQuery()).thenReturn(mockResultSet); when(mockResultSet.next()).thenReturn(true, true, false); try { template.query("SQL", new ColumnMapper<String>() { @Override public String mapColumns(ResultSet resultset) throws SQLException { throw new SQLException(); } }); fail("SQLDaoException expected"); } catch(SQLDaoException ex) { verify(mockResultSet).close(); verify(mockStatement).close(); verify(mockConnection).close(); } }