Generated tests fail in Spring Cloud Contract

1.9k views Asked by At

I am new to spring-cloud-contract. Just trying to configure my build and I am getting error.

Here is my project structure

Project
| ==> Module

I have the following dependencies for classpath in my project build.gradle

Project Build file

buildscript {
dependencies {
    classpath "org.sonarsource.scanner.gradle:sonarqube-gradle-plugin:2.2.1"
    classpath("org.springframework.boot:spring-boot-gradle-plugin:1.5.4.RELEASE")
    classpath "org.springframework.cloud:spring-cloud-contract-gradle-plugin:1.1.3.RELEASE"
}
apply from: "$projectDir/gradle/app/springcloud.gradle" //Gradle build file in module
}

Module Build file

apply plugin: 'groovy'
apply plugin: 'spring-cloud-contract'
apply plugin: 'org.springframework.boot'
dependencies {
//Spring cloud contract dependencies
compile('org.springframework.boot:spring-boot-starter-actuator')
compile('org.springframework.boot:spring-boot-starter-web')
testCompile('org.springframework.boot:spring-boot-starter-test')
testCompile('org.springframework.cloud:spring-cloud-starter-contract-verifier')
testCompile "org.springframework.cloud:spring-cloud-starter-contract-stub-runner"
testCompile "com.jayway.restassured:rest-assured:2.5.0"
testCompile "com.jayway.restassured:spring-mock-mvc:2.5.0"
}
  //Spring cloud contract dependency management
dependencyManagement {
imports {
    mavenBom "org.springframework.cloud:spring-cloud-dependencies:Dalston.RELEASE"
  }
}

I have a groovy file which has a post request and a response. When I do gradle build, the tests are autogenerated but my build is feeling.

C:\Users\user\Projects\myProject\module\build\generated-test-sources\contracts\org\springframework\cloud\contract\verifier\tests\ContractVerifierTest.java:3: error: cannot find symbol
import com.jayway.jsonpath.DocumentContext;
                      ^
symbol:   class DocumentContext
location: package com.jayway.jsonpath

C:\Users\user\Projects\myProject\module\build\generated-test-sources\contracts\org\springframework\cloud\contract\verifier\tests\ContractVerifierTest.java:28: error: cannot find symbol
        DocumentContext parsedJson = 
JsonPath.parse(response.getBody().asString());
        ^

Am I doing something wrong? Is there any mismatch in the versions which I am using?

1

There are 1 answers

0
Vikram On BEST ANSWER

@Marcin Thanks for your inputs. This is how I solved this issue.

The build fails inside generated tests and jayway jsonpath is not showing in Red colour at the position of error. When I Ctrl + Hover (mouse) on the import it's referencing to com.jayway.jsonpath:json-path:2.2.0 which is not true. The reason is because I have imported many modules in Intellij and apparently one of the module also imports 2.2.0 and intellij is referencing it from external library.

So I did invalidate cache intellij and closed all unnecessary projects and reopened the project alone which I am working on, in a new window (Fresh session). Now gradle build and again I got the same error on the generated tests. But this time When I Ctrl + Hover (mouse) on the import its refering to the com.jayway.jsonpath:json-path:0.9.1 which is what I am expecting.

Now to solve this issue, I've examined the dependency tree using the below command and did some grep on it.

allDeps --configuration testRuntime

I found the answer, why gradle is replacing 2.2.0 with 0.9.1. I've figured out some project dependency is replacing it. Now the solution for this is modifying my module build file for jayway json-path as shown below. After that I've given build gradle command and now the errors are gone.

testCompile ('com.jayway.jsonpath:json-path:2.2.0') {
            force = true
}

And finally, I've got exception saying that SpringCloudContractAssertions unable to find.

org.springframework.cloud.contract.verifier.assertion.SpringCloudContractAssertions.assertThat;

So I replace Camden release with Dalston as shown below. Now it all works fine.

dependencyManagement {
    imports {
        mavenBom "org.springframework.cloud:spring-cloud-dependencies:Dalston.RELEASE"
    }
}