複雜業務數據結構,原始測試斷言不那麼直觀,例如如下數據結構,是一個支付業務中,營銷查詢返回結果,按銀行卡掛載優惠信息。
{
"banks": [
{
"bankcardId": 131794663,
"banktype": 36,
"promotionDetails": [
{
"camId": 7918,
"camType": 1,
"merchantId": "11000000044187",
"reduce": "10.00"
},
{
"camId": 7916,
"camType": 1,
"merchantId": "1020700305",
"reduce": "1.00"
}
],
"totalReduce": "11.00"
},
{
"bankcardId": 131799485,
"banktype": 62,
"promotionDetails": [
{
"camId": 7917,
"camType": 1,
"merchantId": "11000000044187",
"reduce": "26.62"
},
{
"camId": 7920,
"camType": 1,
"merchantId": "1020700305",
"reduce": "1.90"
}
],
"totalReduce": "28.52"
}
],
"promotionId": "1578632750975",
"resCode": "SUCCESS",
"resMsg": "SUCCESS"
}
需要對接口返回數據做驗證,驗證某行掛載優惠信息是否正確:
1.結果預處理,化繁爲簡,使用嵌套的蒐集器,並轉換爲指定的數據類型,這裏按銀行類型分組,嵌套蒐集各類型銀行優惠活動id及優惠金額:
private Map<Integer, Map<Integer, String>> getBankCamReduceMap(DiscountInfoResponseDTO discountInfoResponseDTO) {
return discountInfoResponseDTO.getBanks().stream().collect(groupingBy(BankCard::getBanktype,
collectingAndThen(
mapping(bankCard -> {
return bankCard.getPromotionDetails().stream().collect(toMap(PromotionDetailDTO::getCamId, PromotionDetailDTO::getReduce));
}, toSet()),
set -> set.stream().findFirst().get())));
}
2.測試:check reduce info.
@Test(description = "")
public void testBatchQuery_group() throws Exception {
PromoQueryRequest promoQueryRequest = PromoQueryRequestBuilder
.aPromoQueryRequest()
.withChannel("jszf")
.withBusinessId(String.valueOf(genLongSerialNo()))
.withBusinessParams(JSON.toJSONString(businessParams))
.withUser(UserBuilder.anUser().withUserId(34534534643L).build())
.build();
DiscountInfoResponseDTO discountInfoResponseDTO = promoBatchQueryService.batchQuery(promoQueryRequest);
printPrettyJson(discountInfoResponseDTO);
assertThat(discountInfoResponseDTO.getResCode()).isEqualTo("SUCCESS");
assertThat(Long.parseLong(discountInfoResponseDTO.getPromotionId())).isGreaterThan(0);
//check reduce info.
Map<Integer, Map<Integer, String>> bankCamReduceMap = getBankCamReduceMap(discountInfoResponseDTO);
printPrettyJson(bankCamReduceMap);
assertThat(bankCamReduceMap.get(36))
.containsEntry(7916, "1.00")
.containsEntry(7918, "10.00");
assertThat(bankCamReduceMap.get(62))
.hasSize(2)
.containsEntry(7920, "1.90");
assertThat(Double.parseDouble(bankCamReduceMap.get(62).get(7917))).isBetween(21.00, 30.00); //隨機減
boolean totalReduceIsRight = isTotalReduceIsRight(discountInfoResponseDTO);
assertThat(totalReduceIsRight).isTrue();
}
斷言就清爽了許多。