Τετάρτη 28 Απριλίου 2021

Spring Boot @DataJpaTest with simple @Repository stereotype and JdbcTemplate



Spring Boot @DataJpaTest with simple @Repository stereotype







1. DAO implementation with JdbcTemplate

@Repository public class UspNeighboursDAOImpl implements UspNeighboursDAO { @Autowired private JdbcTemplate jdbcTemplate; final String SELECT_QUERY = "select * from usp_neighbours where usp_id = ?"; @Override public List<Map<String, Object>> getNeighboursByUspId(int uspId) { return jdbcTemplate.queryForList(SELECT_QUERY, uspId); } } // DAO Interface public interface UspNeighboursDAO { List<Map<String, Object>> getNeighboursByUspId(int uspId); }


2. Testing

@DataJpaTest @AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE) class ApplicationTests { @Autowired private UspNeighboursDAO uspNeighboursDAO; @Test public void testNeighbours1() { List<?> resList = uspNeighboursDAO.getNeighboursByUspId(2); assertEquals(resList.size(), 2); } }