我失败了:processStandaloneCancelAuthRequest(00, "true")
java.lang.IllegalArgumentException:
参数类型不匹配....错误。我正在使用Mockito进行单元测试并传递00
作为样本输入,而true是样本输出。
@BeforeMethod
public void beforeMethod(){
super.beforeMethod();
authProcessor = new MastercardCancelAuthProcessor();
authProcessor.setPaymentRqIsoAuthRqTransformer(paymentRqIsoAuthRqTransformer);
authProcessor.setIsoAuthRsPaymentRsTransformer(isoAuthRsPaymentRsTransformer);
authProcessor.setAuthDatabaseUpdater(authDatabaseUpdater);
authProcessor.setSsgIsoService(ssgIsoService);
authProcessor.setRemarkGeneratorSelector(remarkGeneratorSelector);
authProcessor.setIsoMessageTransformer(isoMessageTransformer);
authProcessor.setOutMasker(outMasker);
authProcessor.setInMasker(inMasker);
List<Validator<PaymentRQ>> validators = new ArrayList<Validator<PaymentRQ>> ();
validators.add(validator);
authProcessor.setValidators(validators);
requestInfo = new RequestInfo();
requestInfo.setTransactionId(TRANS_ID);
authProcessor.setRequestInfo(requestInfo);
// authProcessor.setUpdateAuthDatabase(true);
paymentRq = new MockPaymentRQFactory().createDefaultPaymentRQ();
Action action = new Action();
action.setType(SubAction.VOID.getValue());
action.setValue(PaymentConstant.CANCEL_AUTH_ACTION);
paymentRq.setAction(action);
}
@DataProvider
public Object[][] createDataProviderForTestingVariousApprovalCodes(){
return new Object[][]{
{ "00", "true"},
{"08", "true"},
};
}
@Test(dataProvider = "createDataProviderForTestingVariousApprovalCodes")
public void processStandaloneCancelAuthRequest(PaymentRQ paymentRq, String transactionId) throws PymtApplicationException {
MastercardCancelIsoMessageRequestHolder req = mock(MastercardCancelIsoMessageRequestHolder.class);
requestInfo.setRequestObject(paymentRq);
when(paymentRqIsoAuthRqTransformer.transform()).thenReturn(req);
SSGDataObject routingInfo = new SSGDataObject();
when(routingInfoCreator.createRoutingInfo(paymentRq.getAction().getValue(),paymentRq.getMerchantDetail().getMerchantID())).thenReturn(routingInfo);
MastercardCancelIsoMessageResponseHolder resp = mock(MastercardCancelIsoMessageResponseHolder.class);
when(ssgIsoService.send(eq(req), eq(isoMessageTransformer), any(PciDataMasker.class), any(PciDataMasker.class), eq(routingInfo))).thenReturn(resp);
PaymentRS paymentRs = createDummyPaymentRS(paymentRq);
when(isoAuthRsPaymentRsTransformer.transformIsoAuthRsToPaymentRs(paymentRq, req, resp)).thenReturn(paymentRs);
when(remarkGeneratorSelector.select(paymentRq)).thenReturn(remarkGenerator);
String remark1 = "REMARK1";
List<String> authRemarks = Arrays.asList(remark1);
when(remarkGenerator.generateRemarks(RemarkType.CANCEL_AUTH, paymentRq, paymentRs)).thenReturn(authRemarks);
PaymentRS actualPaymentRs = authProcessor.processRequest( paymentRq, TRANS_ID);
assertEquals(actualPaymentRs, paymentRs);
verify(validator).validate(paymentRq);
verify(routingInfoCreator).createRoutingInfo(paymentRq.getAction().getValue(),paymentRq.getMerchantDetail().getMerchantID());
verify(paymentRqIsoAuthRqTransformer).transform();
verify(paymentRqIsoAuthRqTransformer).init();
verify(ssgIsoService).send(eq(req), eq(isoMessageTransformer), eq(outMasker), eq(inMasker), eq(routingInfo));
verify(isoAuthRsPaymentRsTransformer).transformIsoAuthRsToPaymentRs(paymentRq, req, resp);
// verify(authDatabaseUpdater).updateAuthRecords(TRANS_ID, paymentRq, req, resp,paymentRs);
verify(remarkGeneratorSelector).select(paymentRq);
verify(remarkGenerator).generateRemarks(RemarkType.CANCEL_AUTH,paymentRq, paymentRs);
}
private PaymentRS createDummyPaymentRS(PaymentRQ paymentRq2) {
// TODO Auto-generated method stub
return null;
}
答案 0 :(得分:2)
我认为你的问题不是由于Mockito造成的。
此测试方法:
public void processStandaloneCancelAuthRequest(PaymentRQ paymentRq, String transactionId)
需要PaymentRQ
个对象和String
。但是,您在String
中返回2 DataProvider
:
@DataProvider
public Object[][] createDataProviderForTestingVariousApprovalCodes(){
return new Object[][]{
{ "00", "true"},
{"08", "true"},
};
}
您应该在PaymentRQ
中返回DataProvider
个对象。
假设PaymentRQ
在其构造函数中将String作为参数,您可以执行以下操作:
@DataProvider
public Object[][] createDataProviderForTestingVariousApprovalCodes(){
return new Object[][]{
{ new PaymentRQ("00"), "true"},
{ new PaymentRQ("08"), "true"},
};
}
答案 1 :(得分:1)
数据提供者没有正确的值。它期待一个PaymentRQ对象和一个字符串对象。 如果PaymentRQ对象需要,则创建一个模拟,并在数据提供者中使用它。
@DataProvider
public Object[][] createDataProviderForTestingVariousApprovalCodes(){
return new Object[][]{
{ <mock object here>, "true"},
{ <mock object here>, "true"},
};
}
同样,在创建模拟对象时,可以为模拟对象上调用的任何方法设置返回值。
when(mock.methodName).thenReturn("00");
等