javasqlmockitorepositoryrowmapper

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"));
    }

}

Solution

  • 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.