英文:
How to mock jdbctemplate query with beanpropertyrowmapper?
问题
question, how can i mock this method ?
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)));
}
This is my current code, all i need to do is find out how to mock the JDBC query method with the arguments above.
@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"));
}
}
英文:
question, how can i mock this method ?
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)));
}
This is my current code, all i need to do is find out how to mock the JDBC query method with the arguments above.
@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"));
}
}
答案1
得分: 2
这应该可以工作:
when(jdbcTemplate.query(yourQuery, new BeanPropertyRowMapper<TarjetaCoordenada>(TarjetaCoordenada.class), yourId)).thenReturn(yourResult)
或者:
when(jdbcTemplate.query(eq(yourQuery), any(), eq(yourId))).thenReturn(yourResult)
将 yourQuery、yourId 和 yourResult 替换为预期的测试值。
英文:
This should work:
when(jdbcTemplate.query(yourQuery, new BeanPropertyRowMapper<>(TarjetaCoordenada.class), yourId)).thenReturn(yourResult)
Or:
when(jdbcTemplate.query(eq(yourQuery), any(), eq(yourId))).thenReturn(yourResult)
Replace yourQuery, yourId and yourResult with the expected test values.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论