Mockito:参数类型不匹配?

Mockito : argument type mismatch?

我失败了: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;
    }

我认为您的问题不是由 Mockito 引起的。

此测试方法:

public void processStandaloneCancelAuthRequest(PaymentRQ paymentRq, String    transactionId) 

需要一个 PaymentRQ 对象和一个 String。然而,你 return 2 Strings in your DataProvider:

@DataProvider
public Object[][] createDataProviderForTestingVariousApprovalCodes(){
   return new Object[][]{
      { "00", "true"},
      {"08", "true"},
    };
}

你应该 return PaymentRQ 个对象在 DataProvider.

所以假设 PaymentRQ 在其构造函数中将一个字符串作为参数,你可以这样做:

@DataProvider
public Object[][] createDataProviderForTestingVariousApprovalCodes(){
   return new Object[][]{
      { new PaymentRQ("00"), "true"},
      { new PaymentRQ("08"), "true"},
    };
}

数据提供者没有正确的值。它需要一个 PaymentRQ 对象和一个字符串对象。 如果需要,为 PaymentRQ 对象创建一个模拟,并在数据提供者中使用它。

@DataProvider
public Object[][] createDataProviderForTestingVariousApprovalCodes(){
   return new Object[][]{
      { <mock object here>, "true"},
      { <mock object here>, "true"},
    };
}

另外,在创建模拟对象时,为可以在模拟对象上调用的任何方法设置 return。

when(mock.methodName).thenReturn("00");

等等