pact-foundation / pact-jvm

JVM version of Pact. Enables consumer driven contract testing, providing a mock service and DSL for the consumer project, and interaction playback and verification for the service provider project.
https://docs.pact.io
Apache License 2.0
1.08k stars 479 forks source link

pactVerify command is not able to verify contracts #1169

Open Sumedh31 opened 4 years ago

Sumedh31 commented 4 years ago

The gradlew pactVerify command does not verify contracts with following setup. Gradle version -5.2.1 "au.com.dius.pact" version "4.1.0" The provider returns asynchronus message (it returns messages by consuming evens from message queues hence not a typical API ) The provider test is as follows:

@Provider("Serviceprovider")
@PactBroker(host = "localhost",port="9292",scheme="http")
@TestTemplate
class serviceprovider{
    @TestTemplate
    @ExtendWith(PactVerificationInvocationContextProvider::class)
    fun testTemplate(pact: Pact<*>, interaction: Interaction, context: PactVerificationContext) {
    context.verifyInteraction()
     }
    @BeforeEach
    fun before(context: PactVerificationContext) {
    context.target = AmpqTestTarget(Collections.emptyList())
    }
   @PactVerifyProvider("interaction")
    fun returnmessage():String{
    "Send command to service and consume emitted event then return the message from that service"
 }

}

The test runs fine when running it via command ' .\gradlew test --tests *serviceprovider' Exception is as follows:

Pact message on console
* What went wrong:  
Execution failed for task ':pactVerify_ProviderService'.    
> class au.com.dius.pact.core.model.messaging.Message cannot be cast to class au.com.dius.pact.core.model.RequestResponseInteraction (au.com.dius.pact.core.model.messaging.Message and au.com.dius.pact.core.model.RequestResponseInteraction are in unnamed module of loader org.gradle.internal.classloader.VisitableURLClassLoader @598b94e6)

Caused by: java.lang.ClassCastException: class au.com.dius.pact.core.model.messaging.Message cannot be cast to class au.com.dius.pact.core.model.RequestResponseInteraction (au.com.dius.pact.core.model.messaging.Message and au.com.dius.pact.core.model.RequestResponseInteraction are in unnamed module of loader org.gradle.internal.classloader.VisitableURLClassLoader @598b94e6)    
at au.com.dius.pact.provider.ProviderVerifier.verifyInteraction(ProviderVerifier.kt:433)    
at au.com.dius.pact.provider.ProviderVerifier.runVerificationForConsumer(ProviderVerifier.kt:593)   
at au.com.dius.pact.provider.ProviderVerifier.runVerificationForConsumer$default(ProviderVerifier.kt:585)   
at au.com.dius.pact.provider.ProviderVerifier.verifyProvider(ProviderVerifier.kt:564)   
at au.com.dius.pact.provider.gradle.PactVerificationTask.verifyPact(PactVerificationTask.groovy:39) 

Note: The publisher configurations is able to publish contracts using gradlew pactPublish command. It seems that the PactVerification enum in providerverifier.kt is not able to set value to "ANNOTATED_METHOD" and hence the failure.

I also checked by adding some logging to see if at least able to acknowledge the @PactVerifyProvider tag or not and it did not.

uglyog commented 4 years ago

Can you try upgrading to the latest version (currently it is 4.1.6)?

Also, if you are using unit tests, you don't need to run the Gradle pactVerify task, as the tests will run as part of the normal unit test run (via Gradle check -> test) tasks.