2017-06-17 121 views
0

我想斷言從方法返回的HashMap getPaymentMethod()與HashMap 預計斷言兩個哈希映射失敗,斷言錯誤

public void testGetPaymentMethod() throws Exception { 
    List<Map<String, Object>> paymentOptionsList = new ArrayList<Map<String, Object>>(); 
    Map<String, Object> pO = new HashMap<String, Object>(); 
    Map<String, Object> pM = new HashMap<String, Object>(); 
    Map<String, Object> capam = new HashMap<String, Object>(); 
    Map<String, Object> preAuthDetail = new HashMap<String, Object>(); 
    preAuthDetail.put("sourceSystem", "ABCD"); 
    preAuthDetail.put("sourceLocation", "EFGH"); 
    preAuthDetail.put("authorizationCode", "OL_DF161216J34491"); 
    capam.put("paymentAmount", 850); 
    capam.put("preAuthDetail", preAuthDetail); 
    pM.put("capam", capam); 
    pO.put("pM",pM); 
    paymentOptionsList.add(pO); 
    @SuppressWarnings("unchecked") 
    HashMap<String, Object> result = (HashMap<String, Object>) myTransformation.getPaymentMethod(paymentOptionsList,null); 
    Map<String, Object> expected = new HashMap<String, Object>(); 
    Map<String, Object> preAuthorizationProfile = new HashMap<String, Object>();   
    Map<String, Object> source = new HashMap<String, Object>(); 
    Map<String, Object> amount = new HashMap<String, Object>(); 
    source.put("sourceSystem", "ABCD"); 
    source.put("sourceLocation", "EFGH"); 
    amount.put("amount", 850); 
    amount.put("monetaryAmount", "UNITEDSTATES_DOLLAR"); 
    preAuthorizationProfile.put("source", source); 
    preAuthorizationProfile.put("preAuthorizationId", "OL_DF161216J34491"); 
    preAuthorizationProfile.put("amount", amount); 
    pM.clear(); 
    pM.put("preAuthorizationProfile", preAuthorizationProfile); 
    expected.put("pM", pM); 
    assertThat(result,is(expected)); 
} 

但是我得到斷言錯誤如下

預期:被< {PAYMENTMETHOD = {preAuthorizationProfile = {量= {量= 850,MONETARYAMOUNT = UNITEDSTATES_DOLLAR},源= {sourceSystem = ABCD ,源 位置= EFGH},preAuthorizationId = OL_DF161216J34491}}}> 但:是< {PAYMENTMETHOD = {preAuthorizationProfile = {源= {sourceSystem = ABCD,sourceLocation = EFGH}},preAuthorizationId = OL_DF161216J34491,一個 安裝= {量= 850,monetaryAmount = UNITEDSTATES_DOLLAR}}}>

+0

期望有一個叫做'amount'的鍵,而實際上有一個'a mount'鍵(注意空格)。 –

+0

@JoeC我想我從cmd提示中複製它,這就是爲什麼它顯示一個空格。 – Beginner

回答

3

我已將您的錯誤消息和格式化爲使您的嵌套地圖級別更清晰。請注意0​​和preAuthorizationId在兩個地圖之間的差異。

Expected: is < 
{ 
    paymentMethod={ 
     preAuthorizationProfile={ 
      amount={ 
       amount=850, 
       monetaryAmount=UNITEDSTATES_DOLLAR 
      }, 
      source={ 
       sourceSystem=ABCD, 
       sourceLocation=EFGH 
      }, 
      preAuthorizationId=OL_DF161216J34491 
     } 
    } 
} 
> but: was < 
{ 
    paymentMethod={ 
     preAuthorizationProfile={ 
      source={ 
       sourceSystem=ABCD, 
       sourceLocation=EFGH 
      } 
     }, 
     preAuthorizationId=OL_DF161216J34491, 
     amount={ 
      amount=850, monetaryAmount=UNITEDSTATES_DOLLAR 
     } 
    } 
} 
> 

可能我建議,而不是使用地圖的地圖的地圖,試圖想出代表你想要做什麼班。這會使你的代碼更具彈性,並且更不容易出現這樣的錯誤。