[英]How to JSON-marshal JAXBElement-wrapped responses without the JAXBElement wrapper?
[英]How to mock an object wrapped in a JAXBElement
考慮一下JAXB生成的以下代碼:
public class CnpOnlineResponse {
protected JAXBElement<? extends TransactionTypeWithReportGroup> transactionResponse;
public JAXBElement<? extends TransactionTypeWithReportGroup> getTransactionResponse() {
return transactionResponse;
}
public void setTransactionResponse(JAXBElement<? extends TransactionTypeWithReportGroup> value) {
this.transactionResponse = value;
}
}
public class AuthorizationResponse extends TransactionTypeWithReportGroup {
}
public class ObjectFactory {
public JAXBElement<AuthorizationResponse> createAuthorizationResponse(AuthorizationResponse value) {
return new JAXBElement<AuthorizationResponse>(_AuthorizationResponse_QNAME, AuthorizationResponse.class, null, value);
}
}
我正在編寫一個測試,其中我同時模擬了CnpOnlineResponse和AuthorizationResponse:
@Mock private CnpOnlineResponse mockCnpOnlineResponse;
@Mock private AuthorizationResponse mockAuthorizationResponse;
final JAXBElement<AuthorizationResponse> authorization = CnpContext.getObjectFactory().createAuthorizationResponse(mockAuthorizationResponse);
when(mockCnpOnlineResponse.getTransactionResponse()).thenReturn(authorization); // <= ERROR here
但是我收到以下編譯錯誤:
The method thenReturn(JAXBElement<capture#1-of ? extends TransactionTypeWithReportGroup>) in the type OngoingStubbing<JAXBElement<capture#1-of ? extends TransactionTypeWithReportGroup>> is not applicable for the arguments (JAXBElement<AuthorizationResponse>)
任何幫助表示贊賞,我不明白為什么它認為沒有將thenReturn傳遞給所需類型的對象。
似乎混淆了類型推斷。
使用thenAnswer
(或短then
)。
when(mockCnpOnlineResponse.getTransactionResponse()).then(i -> authorization);
i -> authorization
是評估為Answer<? extends TransactionTypeWithReportGroup>
的lambda表達式Answer<? extends TransactionTypeWithReportGroup>
Answer<? extends TransactionTypeWithReportGroup>
使其能夠匹配的捕獲。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.