Mockito ArgumentCaptor在JUnit5 Pact契约测试中失败
我发现一个奇怪的行为时使用Pact与Mockito (JUnit5):
我想在测试中获得Manager类的Client.send(payload)
中payload
的内容:
public void send(Event event){
var payload = new SendEvent(event);
client.send(payload);
}
所以在测试中,我们通常可以检查Client.send(payload)
调用,没有任何问题:
manager.sendEvent(foo);
verify(client).send(payload);
虽然使用ArgumentCaptor
获取内容结果如下:
@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 '...'
有人知道这是一个已知的问题吗?当使用Pact
时,基本上是不可能抓取内容的。同样的代码,只有纯Mockito
没有任何缺陷。
全部加:
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
所以看起来ArgumentCaptors不能与Pact一起使用?
编辑:更正了ArgumentCaptor并添加了全栈跟踪
我找到了解决办法:
@State("state")
@PactVerifyProvider("pactVerifyProvider")
public String pactEvent(){
manager.sendEvent(foo);
verify(client).send(captor.capture());
}
@State
需要引用一个空方法,所以设置一个独立于@PactVerifyProvider
的空状态。