Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Junit 5 producer test failed when combine async and sync test #1353

Closed
miaguicam7 opened this issue Apr 28, 2021 · 3 comments
Closed

Junit 5 producer test failed when combine async and sync test #1353

miaguicam7 opened this issue Apr 28, 2021 · 3 comments

Comments

@miaguicam7
Copy link

Junit 5 consumer test fails when we combine async RabbitMQ test with http test. If we separate the test, it runs successfully. Below is an example class to replicate this.

`@Test
@PactTestFor(pactMethod = "saveProjectInvest", providerType = ProviderType.ASYNCH)
void saveProjectInvestTest(List messageList) throws JsonProcessingException {
assertNotNull(messageList);

    Message pactMessage = getFirstMessage(messageList);
    SaveProjectInvestResponse response = convertPactMessageToInvestorAnswer(pactMessage);

    assertNotNull(response);
    assertNotNull(response.getHasError());
    assertNotNull(response.getProject());
    assertNotNull(response.getErrorData());
}

@Pact(consumer = "investor", provider = "project")
public MessagePact saveProjectInvest(MessagePactBuilder builder) {

    SaveProjectInvestResponse investorAnswerMock =  new EasyRandomBuilder()
            .build().nextObject(SaveProjectInvestResponse.class);

    return builder
            .expectsToReceive("a project answer")
            .withContent(
                    ConsumerExpects.type(SaveProjectInvestResponse.class)
                            .useTypeMapping(AsyncErrorRecived.class, PactBodyCustomMappings.asyncErrorRecivedMapping())
                            .useTypeMapping(Date.class, PactBodyCustomMappings.dateMapping())
                            .ignoreMissingValues()
                            .build(investorAnswerMock))
            .toPact();
}

private Message getFirstMessage(final List<Message> messageList) {
    return messageList.stream().findFirst().orElseThrow(RuntimeException::new);
}

private SaveProjectInvestResponse convertPactMessageToInvestorAnswer(final Message pactMessage) throws JsonProcessingException {
    return mapper.readValue(pactMessage.contentsAsString(), SaveProjectInvestResponse.class);
}

@Test
@PactTestFor(pactMethod = "getProjectByIdPact")
void getProjectByIdPactNotNullTest(MockServer mockServer) {
    Project project = restTemplate.getForObject(mockServer.getUrl() + "/projects/1234567", Project.class);
    assertNotNull(project);
}

@Pact(consumer = "investor", provider = "project")
public RequestResponsePact getProjectByIdPact(PactDslWithProvider builder) {
    return builder
            .given("Project 1234567 exists")
            .uponReceiving("A request to /projects/1234567")
            .path("/projects/1234567")
            .method("GET")
            .willRespondWith()
            .status(200)
            .body(ConsumerExpects.type(Project.class)
                    .useTypeMapping(Date.class, (body, fieldName, fieldValue) ->
                            body.date(fieldName, "yyyy-MM-dd'T'HH:mm:ss.SSSZ"))
                    .ignoreMissingValues()
                    .build(new PactDslJsonBody(), project)
            )
            .toPact();
}`

Running the above pact test fails with the following stack trace:

The following methods annotated with @Pact were not executed during the test: ProjectTest.validateProjectPact, ProjectTest.updateBrickfundingProjectPact, ProjectTest.getProjectByIdPact, ProjectTest.saveProjectInvestPact
If these are currently a work in progress, add a @disabled annotation to the method

java.lang.AssertionError: The following methods annotated with @Pact were not executed during the test: ProjectTest.validateProjectPact, ProjectTest.updateBrickfundingProjectPact, ProjectTest.getProjectByIdPact, ProjectTest.saveProjectInvestPact
If these are currently a work in progress, add a @disabled annotation to the method

at au.com.dius.pact.consumer.junit5.PactConsumerTestExt.afterAll(PactConsumerTestExt.kt:431)
at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.lambda$invokeAfterAllCallbacks$13(ClassBasedTestDescriptor.java:421)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.lambda$invokeAfterAllCallbacks$14(ClassBasedTestDescriptor.java:421)
at java.util.ArrayList.forEach(ArrayList.java:1259)
at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.invokeAfterAllCallbacks(ClassBasedTestDescriptor.java:421)
at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.after(ClassBasedTestDescriptor.java:213)
at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.after(ClassBasedTestDescriptor.java:77)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$6(NodeTestTask.java:145)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7(NodeTestTask.java:145)
at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:135)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:123)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:122)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:80)
at java.util.ArrayList.forEach(ArrayList.java:1259)
at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:38)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:139)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7(NodeTestTask.java:125)
at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:135)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:123)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:122)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:80)
at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.submit(SameThreadHierarchicalTestExecutorService.java:32)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.execute(HierarchicalTestExecutor.java:57)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestEngine.execute(HierarchicalTestEngine.java:51)
at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:170)
at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:154)
at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:90)
at org.gradle.api.internal.tasks.testing.junitplatform.JUnitPlatformTestClassProcessor$CollectAllTestClassesExecutor.processAllTestClasses(JUnitPlatformTestClassProcessor.java:92)
at org.gradle.api.internal.tasks.testing.junitplatform.JUnitPlatformTestClassProcessor$CollectAllTestClassesExecutor.access$100(JUnitPlatformTestClassProcessor.java:77)
at org.gradle.api.internal.tasks.testing.junitplatform.JUnitPlatformTestClassProcessor.stop(JUnitPlatformTestClassProcessor.java:73)
at org.gradle.api.internal.tasks.testing.SuiteTestClassProcessor.stop(SuiteTestClassProcessor.java:61)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:35)
at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
at org.gradle.internal.dispatch.ContextClassLoaderDispatch.dispatch(ContextClassLoaderDispatch.java:32)
at org.gradle.internal.dispatch.ProxyDispatchAdapter$DispatchingInvocationHandler.invoke(ProxyDispatchAdapter.java:93)
at com.sun.proxy.$Proxy2.stop(Unknown Source)
at org.gradle.api.internal.tasks.testing.worker.TestWorker.stop(TestWorker.java:131)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:35)
at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
at org.gradle.internal.remote.internal.hub.MessageHubBackedObjectConnection$DispatchWrapper.dispatch(MessageHubBackedObjectConnection.java:155)
at org.gradle.internal.remote.internal.hub.MessageHubBackedObjectConnection$DispatchWrapper.dispatch(MessageHubBackedObjectConnection.java:137)
at org.gradle.internal.remote.internal.hub.MessageHub$Handler.run(MessageHub.java:404)
at org.gradle.internal.concurrent.ExecutorPolicy$CatchAndRecordFailures.onExecute(ExecutorPolicy.java:63)
at org.gradle.internal.concurrent.ManagedExecutorImpl$1.run(ManagedExecutorImpl.java:46)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at org.gradle.internal.concurrent.ThreadFactoryImpl$ManagedThreadRunnable.run(ThreadFactoryImpl.java:55)
at java.lang.Thread.run(Thread.java:748)

dependencies:
testCompile 'au.com.dius.pact.consumer:junit5:4.1.19'
testCompile 'au.com.dius.pact.provider:junit5spring:4.1.19'

thanks so much!

@pendsley
Copy link
Contributor

This is currently a known limitation of Pact. Message and http pacts must be separate pacticipants; you need separate contracts for each type.

@miaguicam7
Copy link
Author

Perfect, thanks so much for your answer!!

@mefellows
Copy link
Member

See also pact-foundation/pact-specification#71 and specifically this feature: pact-foundation/pact-specification#79

So that would imply it's coming soon to this project, if not already available via some interface.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants