javagradlegradle-pluginspring-cloud-contract

Unable to generate contract tests when using spring-cloud-contract-oa3 and spring cloud contract gradle plugin


When when I specify the contract in an OA3 yml file and then build the project the build passes but it doesn't generate the contract tests . If I replace the OA3 yml contract file with a groovy equivalent then the build passes and it generates and runs the contract tests.

I have the following setup:

my build.gradle looks like:

buildscript {
    repositories {
        mavenCentral()
        maven { url 'https://jitpack.io' }
    }
    dependencies {
        classpath 'org.springframework.cloud:spring-cloud-contract-gradle-plugin:3.1.2'
        classpath 'com.github.mzielinski:spring-cloud-contract-oa3:3.1.2.0'
    }
}

plugins {
    id 'org.springframework.boot' version '2.6.8'
    id 'io.spring.dependency-management' version '1.0.11.RELEASE'
    id 'java'
    id 'war'
    id 'java-library'
    id 'maven-publish'
    id 'com.adarshr.test-logger' version '3.0.0'
    id "org.unbroken-dome.test-sets" version "4.0.0"
    id 'groovy'
}

apply plugin: 'spring-cloud-contract'

testSets {
    contractTest
}


ext {
    springBootStarterVersion = '2.6.8'
    springCloudVersion = '2021.0.3'
    projectLombokVersion = '1.18.24'
}

repositories {
    mavenLocal()
    mavenCentral()
}

dependencies {
    
    testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.2'

    testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine'

    testImplementation unitTesting

    testImplementation 'org.springframework.cloud:spring-cloud-starter-contract-verifier'
    
    compileOnly "org.projectlombok:lombok:${projectLombokVersion}"
    annotationProcessor "org.projectlombok:lombok:${projectLombokVersion}"
    testAnnotationProcessor "org.projectlombok:lombok:${projectLombokVersion}"
}

dependencyManagement {
    imports {
        mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
    }
}

test {
    useJUnitPlatform()
}

contractTest {
    useJUnitPlatform()
}

contracts {
    testFramework = org.springframework.cloud.contract.verifier.config.TestFramework.JUNIT5
    baseClassForTests = 'com.my.project.contract.BaseContractTest'
    testMode = 'MockMvc'
    contractsPath = 'src/contractTest/resources/contracts'
}

}

Solution

  • @Lamminski, I guess, I was able to reproduce your issue.

    First issue is with 'com.github.mzielinski:spring-cloud-contract-oa3:3.1.2.0' in buildSrc. Please, move it into testImplementation. Then you should be able to see generated contracts in /tmp/sccoa3___.yml files

    Generally, spring-cloud-contract-oa3 converts open-api into yaml contracts in /tmp directory and then such files are used for generating contract DSLs with YmlContract class from SSC.

    Additionally, I noticed also using my example from https://github.com/mzielinski/spring-cloud-contract-oa3/blob/develop/src/test/resources/openapi/verify_oa3.yml and your build.gradle file, that scc has a problem to convert properly contracts generated from this file in two places (to tests):

    In SPOCK for example generated test has such code

    assertThatJson(parsedJson).field("['request']").isEqualTo("{"path":"/etc/passwd"}")
    

    There is invalid escaping. Generated contract has

      body:
        path: "/etc/passwd"
    

    so looks OK.

    I am using sscoa3 in production and I did not have such issue yet, but I will try to fix YamlContract in SSC to convert it in proper way.

    You are able to see stacktrace from YmlContract also by running gradle build with --debug flag.