JUnit Integration Testing NamedJdbcTemplate

119 Views Asked by At

I have a Repository class RaRepository as below

@Repository
public class RaRepository{
@Autowired
private NamedParameterJdbcTemplate jdbcTemplate;

public List<raAttribute> findFieldsByItemId(list<string> itemsId){
String sql = "select * from abc where id in (:itemsId)";
SqlParameterSource param = new MapSqlParameterSource("itemsId",itemsId);
List<raAtrribute> raAttList = jdbcTemplate.query(sql, param, 
(rs, rowNum) -> new raAtrribute(rs.getString(ps_name)));

return raAttList;
}

}

I have written a Junit Test class as below

public class RaRepositoryTest {
@Autowired 
Private RaRepository raRepository;

@Test
public void findFieldsByItemId(){
List<String> itemsId = new ArrayList<>();
itemsId.add("VZ203")
List<raAttribute> raAttlist = raRepository.findFieldsByItemId(itemsId);
Assert.assertEquals(2, raAttlist.size);
}

The datasource written in application.properties file. I need to connect to actual db and run tests. I have tried with annotations like @RunWith(SpringRunner.class), @DataJdbcTest etc. I am new to jdbctemplate and not sure how to write test case for above scenario to achieve db connectivity.

Note: Iam using JUnit4 andService class which actually calls findFieldsByItemId method have separate mocking unit test.

0

There are 0 best solutions below