InvalidUseOfMatchersException при вставке в Groovy - PullRequest
0 голосов
/ 05 августа 2020

У меня есть подпись метода Java вроде этого:

public Resource generatePDF(
  List<InvoiceReportEntity> invoices,
  String labelerCd,
  InvoiceContactEntity contact,
  CalculatedInvoiceAmount calculatedInvoice,
  InvoicePaymentEntity invoicePaymentsAddress,
  List <InvoiceReportPpaEntity> allQuartersInvoicePpaList,
  BigDecimal currQtrInterestAmt,
  List<InvoicePpaInterest> intAmt,
  Integer invoiceYearQtr
)

Вот мой модульный тест, написанный на Groovy:

def '10.Export PDF with valid id'() {
    given:'Valid id'
    def invoiceIds = ['AL4-00008-20192']
    def types = [ExportType.PDF] as ExportType[]

    and:
    def mockInvoicePDFProcessor = mock(InvoicePDFProcessor)

    when(
      mockInvoicePDFProcessor.generatePDF(
        any(List.class),
        anyString(),
        any(InvoiceContactEntity.class),
        any(CalculatedInvoiceAmount.class),
        any(InvoicePaymentEntity.class),
        any(List.class),
        any(BigDecimal.class),
        any(List.class),
        any(Integer.class)
      )
    )
    .thenReturn(mock(Resource));
    invoiceController.setPdfProcessor(mockInvoicePDFProcessor)

    when: 'Retrieve results based on invoice header id'
    def response = invoiceController.exportInvoiceData(
      types, invoiceIds,
      mock(HttpServletResponse)
    )

    then: 'response received'
    response.getStatusCode().value() == 200
    response.hasBody()
}

Этот тест проходит при индивидуальном запуске, но когда запускаются все тестовые примеры, я получаю это исключение

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: 
Misplaced or misused argument matcher detected here:
-> at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallStatic(CallSiteArray.java:56)
-> at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallStatic(CallSiteArray.java:56)
-> at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallStatic(CallSiteArray.java:56)
-> at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallStatic(CallSiteArray.java:56)
You cannot use argument matchers outside of verification or stubbing.
Examples of correct usage of argument matchers:
    when(mock.get(anyInt())).thenReturn(null);
    doThrow(new RuntimeException()).when(mock).someVoidMethod(anyObject());
    verify(mock).someMethod(contains("foo"))

This message may appear after an NullPointerException if the last matcher is returning an object 
like any() but the stubbed method signature expect a primitive argument, in this case,
use primitive alternatives.
    when(mock.get(any())); // bad use, will raise NPE
    when(mock.get(anyInt())); // correct usage use
Also, this error might show up because you use argument matchers with methods that cannot be mocked.
Following methods *cannot* be stubbed/verified: final/private/equals()/hashCode().
Mocking methods declared on non-public parent classes is not supported.
    at 10.Export PDF with valid id(InvoiceControllerSpec.groovy:222)**

Я использовал .class для всех классов, использованных выше, все равно результат тот же. Я искал похожие ошибки, но, к сожалению, упомянутое здесь решение не работает для меня:

...