简体   繁体   中英

Mockito fails with Pact contract tests

Mockito ArgumentCaptor fails with JUnit5 Pact contract tests

I found out a weird behaviour when using Pact with Mockito (JUnit5):

I'd like to get the content of payload in Client.send(payload) of the Manager class in the tests:

public void send(Event event){

    var payload = new SendEvent(event);
    client.send(payload);
  }

So in the tests normally we can check the Client.send(payload) call with, what works without any problems:

manager.sendEvent(foo);
verify(client).send(payload);

Although using ArgumentCaptor to get the content results in following error:

@Captor
private ArgumentCaptor<Event> captor;

@State("state")
@PactVerifyProvider("pactVerifyProvider")
public String pactEvent(){
   manager.sendEvent(foo);
   verify(client).send(captor.capture());
}


=> Verification Failed - Failed to invoke provider method '...'

Does anyone know if this is a known problem? Basically it is not possible to grab content when using Pact . The same code with only plain Mockito works without any flaws.

Full Stacktrace:



Event
    generates a message which
    Verification Failed - Failed to invoke provider method 'sendEvent'

...


java.lang.AssertionError: Service - Generates message 'event'  
Failures:

1) Event

    1.1) Failed to invoke provider method 'sendEvent'

    at au.com.dius.pact.provider.junit5.PactVerificationContext.verifyInteraction(PactVerificationContext.kt:63)
    at test.ContractTest.testTemplate(ProviderContractTest.java:80) -> thats where "context.verifyInteraction();"" is used
    at java.base/java.util.stream.ForEachOps$ForEachOp$OfRef.accept(ForEachOps.java:183)
    at java.base/java.util.ArrayList$ArrayListSpliterator.forEachRemaining(ArrayList.java:1655)
    at java.base/java.util.ArrayList$ArrayListSpliterator.forEachRemaining(ArrayList.java:1655)
    at java.base/java.util.stream.ForEachOps$ForEachOp.evaluateSequential(ForEachOps.java:150)
    at java.base/java.util.stream.ForEachOps$ForEachOp$OfRef.evaluateSequential(ForEachOps.java:173)
    at java.base/java.util.stream.ReferencePipeline.forEach(ReferencePipeline.java:497)
    at java.base/java.util.ArrayList.forEach(ArrayList.java:1541)
    at java.base/java.util.ArrayList.forEach(ArrayList.java:1541)

Process finished with exit code -1

So it seems that the ArgumentCaptors cannot be used with Pact?

EDIT: corrected ArgumentCaptor and added full stack trace

I found out the solution:

    @State("state")
    @PactVerifyProvider("pactVerifyProvider")
    public String pactEvent(){
       manager.sendEvent(foo);
       verify(client).send(captor.capture());
}

@State needed to refer to an empty method, so set an empty state independent of the @PactVerifyProvider .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM