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

java.lang.NoSuchMethodError for latest java8 junit5 when using both consumer and provider in project #1472

Closed RHervey-eb closed 2 years ago

RHervey-eb commented 2 years ago

Expected behavior -> When running junit5 using the latest Pact version for java 8 in IDE, be able to run the consumer and provider tests (separate run for each) as well as on the command line (using mvn and only including the requisite test classes).

Actual behavior -> Running the test using the IDE or mvn produces the following error:

[ERROR] test_full_consumer_for_event{List}  Time elapsed: 0.033 s  <<< ERROR!
java.lang.NoSuchMethodError: au.com.dius.pact.core.support.expressions.ExpressionParser.parseExpression$default(Ljava/lang/String;Lau/com/dius/pact/core/support/expressions/DataType;Lau/com/dius/pact/core/support/expressions/ValueResolver;ZILjava/lang/Object;)Ljava/lang/Object;

When I exclude the dependency au.com.dius.pact.core:support then I am able to run the consumer test (but, obviously not the provider tests)

Versions used:

<dependency>
    <groupId>au.com.dius.pact.consumer</groupId>
    <artifactId>junit5</artifactId>
    <version>4.1.28</version>
    <scope>test</scope>
</dependency>
<dependency><!-- Added to allow for the LambdaDsl configuration -->
    <groupId>au.com.dius</groupId>
    <artifactId>pact-jvm-consumer-java8</artifactId>
    <version>4.0.10</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>au.com.dius.pact.provider</groupId>
    <artifactId>junit5</artifactId>
    <version>4.1.28</version>
    <scope>test</scope>
</dependency>

Looking at the code changes, it appears the class and method that's missing the expected argument is in the ExpressionParser.kt.

Link to the change: https://github.com/pact-foundation/pact-jvm/commit/d3f49648c064ace5e95334ffe6faa6088ec812e9#diff-60f5a5b831e279e4b778ce72ce319289d154ab60ec3535191a0737371c817c8d

Please let me know how to be able to run both consumer and provider tests (I'm doing Async validation if that helps) in the same project.

uglyog commented 2 years ago

Please remove pact-jvm-consumer-java8, you don't need it and it is causing your issue.

RHervey-eb commented 2 years ago

Thanks so much for a quick response.

If I do that, is there another place where I can get the ability to use the Lambda Dsl which is indeed much easier to manage/read?

I'm referring to this: https://docs.pact.io/implementation_guides/jvm/consumer#a-lambda-dsl-for-pact

RHervey-eb commented 2 years ago

@uglyog My direct question is, how do I use the Lambda Dsl if I'm using Junit 5, and Java 8 with a provider and consumer?

Is that combination supported. If it is, how do I set that up (what dependencies, etc)?

uglyog commented 2 years ago

JDK 8 lambda support is included by default (as the minimum is now JDK 11). You don't need a separate library for it.

The junit5 will pull in the required library as a dependency.

RHervey-eb commented 2 years ago

@uglyog I see that the LambdaDsl class is added if I use the JDK 11+ dependencies (4.2.x). This doesn't work for us since we are running Java 8 (using the 4.1.x) dependencies.

How do I get LambdaDsl to work with the producer/consumer setup given that I am running JDK8 (not 9 or later)?

To put it simply, when I have those 2 dependencies in my pom file, the LambdaDsl class is not found. How do you suggest we fix this?

RHervey-eb commented 2 years ago

The solution was simple, the needed dependencies list that worked is: (turns out the dependency artifact and group being off was the culprit) :)

        <dependency>
            <groupId>au.com.dius.pact.consumer</groupId>
            <artifactId>junit5</artifactId>
            <version>4.1.28</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>au.com.dius.pact.consumer</groupId>
            <artifactId>java8</artifactId>
            <version>4.1.28</version>
        </dependency>
        <dependency>
            <groupId>au.com.dius.pact.provider</groupId>
            <artifactId>junit5</artifactId>
            <version>4.1.28</version>
            <scope>test</scope>
        </dependency>