Skip to content

Generating tests from Jar contracts doesn't work #391

@rzukowski-equinix

Description

@rzukowski-equinix

Hi,

So I'm trying to generate contract tests from Groovy DSL that are packaged in a Jar (similar to producer_with_external_contracts example).

I packaged the Jar as shown in beer_contracts example - meaning that groovy DSL files are placed in root of a Jar.
Then the Jar is published to remote repository.

In another project I'm trying to get that Jar artifact and based on DSLs generate tests using Gradle like so:


plugins {
    id 'java'
    id 'groovy'
    id "maven-publish"
    id 'idea'
    id 'org.springframework.boot' version "2.7.1"
    id 'io.spring.dependency-management' version '1.0.11.RELEASE'
    id "io.freefair.lombok" version '6.3.0'
    id "org.springframework.cloud.contract" version "3.1.3"
}

contracts {
    testFramework = 'Spock'
    testMode = 'EXPLICIT'
    baseClassForTests = 'example.E2EBaseITest'
    contractDependency {
        stringNotation = 'example:0.0.17:stubs'
    }
    contractRepository {
        repositoryUrl =  'urlToRepo'
        username = "${mvnUsername}"
        password = "${mvnPassword}"
    }
    contractsMode = "REMOTE"
}

dependencyManagement {
    imports {
        mavenBom "org.springframework.boot:spring-boot-dependencies:2.7.1"
        mavenBom "org.springframework.cloud:spring-cloud-dependencies:2021.0.3"
        mavenBom "org.springframework.cloud:spring-cloud-contract-dependencies:3.1.3"
        mavenBom "org.spockframework:spock-bom:2.0-groovy-3.0"
    }
}

//omitted dependency section

sourceSets {
    test {
        groovy.srcDirs += "${project.buildDir}/generated-test-sources/contracts"
    }
}


test {
    useJUnitPlatform()
    testLogging {
        events("passed", "skipped", "failed")
        exceptionFormat = 'full'
    }

    afterSuite { desc, result ->
        if (!desc.parent) {
            println "##### RESULT: " +
                    "${result.testCount} TOTAL, " +
                    "${result.successfulTestCount} SUCCESSES, " +
                    "${result.failedTestCount} FAILURES, " +
                    "${result.skippedTestCount} SKIPPED " +
                    "#####"
        }
    }
}

tasks.publish.dependsOn tasks.build
tasks.build.dependsOn tasks.test
tasks.test.dependsOn tasks.compileTestGroovy
tasks.compileTestGroovy.dependsOn tasks.generateContractTests

But the tests are not generated, no catalogs with tests are created, nothing happens...
I'm sure it gets the Jar from repo, because if I change the name to the wrong one, then I get error that it can't get the artifact.

EDIT:
I dug a little more and potentialy I see the problem.
The artifactId of a contract is completly different than the artifactId of project using it.
Let's say contract is 'com.example:contract:1.0:stubs' and the project using it 'com.example:project:2.0'.

When I look into gradle logs of copyContracts there seems to be an error there:

Unpacking stub from JAR [URI: file:/var/folders/tq/2shwm95s22j6ff4k7qkdj4tr0000gp/T/aether-local13676370951254601311/com/example/contract/1.0/com-example-contract-1.0-stubs.jar]
Unpacked file to [/var/folders/tq/2shwm95s22j6ff4k7qkdj4tr0000gp/T/contracts-1658399245296-0]
Pattern to pick contracts equals [^/var/folders/tq/2shwm95s22j6ff4k7qkdj4tr0000gp/T/contracts-1658399245296-0(/)?.*com/example/project/.*$]
Ant Pattern to pick files equals [**/com/example/project/**/]

Why it's setting the pattern to "**com/example/project/" which is current project artifactId and not the downlowded jar artifact id? It should be "**com/example/contract/"

EDIT 2:
After fiddling around a bit more it turns out I can alter the directory from which the contract DSL files will be copied from by setting the configuration option contractsPath, i.e.:
contractsPath = "/" - this will copy contract files correctly in case of contracts shown in beer_contracts example.

The problem is that in the documentation https://cloud.spring.io/spring-cloud-contract/reference/html/gradle-project.html it's described as: "Specifies the path to the jar." Which is wrong because the it's the location of a catalog of extracted JAR.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions