1

请帮我模拟下面的代码。无法模拟调用 JdbcTemplate 对象的 getDataSource()。

@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;
                }
            });
}
4

1 回答 1

1

您使用的是什么 Mock 框架?

如果您使用 Mockito,只需 Mock thejdbcTemplate并将此方法放在when()子句中。

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

您必须admDatabaseConnector在 Mock 中声明。

于 2017-03-08T12:45:49.480 回答