问题,我如何模拟这个方法?
return jdbcTemplate.query(query.toString(), new BeanPropertyRowMapper<>(TarjetaCoordenada.class), id);
@Override
public <T> List<T> query(String sql, RowMapper<T> rowMapper, @Nullable Object... args) throws DataAccessException {
return result(query(sql, args, new RowMapperResultSetExtractor<>(rowMapper)));
}
这是我目前的代码,我所需要做的就是找出如何用上面的参数模拟JDBC查询方法。
@ExtendWith(MockitoExtension.class)
class TipoEstadoRepositoryTests {
@Mock
private JdbcTemplate jdbcTemplate;
@InjectMocks
private TipoEstadoRepository repository;
@Test
void shouldValidateConsultar() {
when(repository.consultar(Mockito.anyString())).thenReturn(null);
Assertions.assertNull(repository.consultar("abc"));
}
}
这应该有效:
when(jdbcTemplate.query(yourQuery, new BeanPropertyRowMapper<>(TarjetaCoordenada.class), yourId)).thenReturn(yourResult)
或者:
when(jdbcTemplate.query(eq(yourQuery), any(), eq(yourId))).thenReturn(yourResult)
用预期的测试值替换yourQuery、yourId和yourResult。