1

Please help me to mock below code .Not able to mock getDataSource() which is calling upon JdbcTemplate object.

@Override
public List<AttributeThresholdRange> getThresholdsRangeForXHS(QueryThresholdsRequest queryThresholdsRequest) {

    ArrayOfString attributeGroupIds = queryThresholdsRequest.getAttributeGroupIds();
    Map<String, Object> queryParams = new HashMap<>();
    queryParams.put("groupids", attributeGroupIds.getStrings());

    return new NamedParameterJdbcTemplate(admDatabaseConnector.getJdbcTemplate().getDataSource())
            .query(DBQueryConstants.ADM_QUERY_GET_THRESHOLDS_RANGE_FOR_XHS, 
                    queryParams,
                    new ResultSetExtractor<List<AttributeThresholdRange>>() {
                @Override
                public List<AttributeThresholdRange> extractData(ResultSet resultSet) throws SQLException,DataAccessException  {

                    return null;
                }
            });
}
Gaurav
  • 33
  • 2
  • 11

1 Answers1

1

what´s the Mock framework are you using?

If you are using Mockito just Mock the jdbcTemplate and put this method in the when() clause.

when(admDatabaseConnector.getJdbcTemplate().getDataSource())
            .query(anyObject(), anyObject(), anyObject())).thenReturn("Your return queryobject");

You must declare the admDatabaseConnector in the Mock.

Brad
  • 15,186
  • 11
  • 60
  • 74