Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring cloud contracts plugin change sourceset

I've started using Spring Cloud Contracts ('2.0.2.RELEASE') in my project and I have the following structure

src
|
  -- main
  -- test
  -- integrationTest
  -- contractTest

When I put my contracts and my base test class in test it was running fine. I want to move the contract tests that I have written into a separate sourceset, the contractTest sources. However, this will not work as the plugin generateContractTests task will still look in the test sourceset when trying to run.

I have made the following changes to my Gradle file

task contractTest(type: Test) {
    description = 'Runs contract tests.'
    group = 'verification'

    testClassesDirs = sourceSets.contractTest.output.classesDirs
    classpath = sourceSets.contractTest.runtimeClasspath
    shouldRunAfter integrationTest
}
configurations {
    contractTestImplementation.extendsFrom implementation
    contractTestRuntimeOnly.extendsFrom runtimeOnly
}
sourceSets {
    contractTest {
        compileClasspath += sourceSets.main.output
        runtimeClasspath += sourceSets.main.output
    }
}
contracts {
//    testFramework = 'JUNIT5'
    packageWithBaseClasses = 'com.test.testapi.contracts'
    contractsDslDir = new File("${project.rootDir}/src/contractTest/resources/contracts/")
}
contractTestImplementation 'org.codehaus.groovy:groovy-all:2.4.6'
contractTestImplementation 'org.springframework.cloud:spring-cloud-starter-contract-verifier'
contractTestImplementation group: 'org.junit.jupiter', name: 'junit-jupiter-api'
contractTestImplementation group: 'org.junit.jupiter', name: 'junit-jupiter-engine'

I think I need to set the contract plugin property contractDependency, however, I am not sure and can't find an example to get the plugin to work with this different sourceset

TLDR; I want to be able to run my contract tests in a different folder

UPDATE - I am not sure but I think that it is not possible as in the Gradle plugin in the "GenerateServerTestsTask.groovy" file has the following which would appear to signify that the sourceSet is hardcoded to test throughout the code

project.sourceSets.test.groovy {
    project.logger.
            info("Registering ${getConfigProperties().generatedTestSourcesDir} as test source directory")
    srcDir getConfigProperties().getGeneratedTestSourcesDir()
}
like image 445
bubblebath Avatar asked Feb 17 '26 11:02

bubblebath


1 Answers

For future reference, I was able to get it working by creating a custom task to deregister the generated sources from the test sourceset so that they wouldn't be compiled by compileTestJava and could be run via my own contractTests task.

sourceSets {
     contractTest {
        java {
            compileClasspath += sourceSets.main.output + sourceSets.test.output
            runtimeClasspath += sourceSets.main.output + sourceSets.test.output
            srcDir file('src/contractTest/java')
            srcDirs += file("${buildDir}/generated-contract-sources/")
        }
        resources.srcDir file('src/contractTest/resources')
    }
}

task deregisterContractTestSources() {
    doLast {
        project.sourceSets.test.java {
            project.logger.info('Removing any *Spec classes from the test sources')
            exclude '**/*Spec*'
        }
    }
}
compileTestJava.dependsOn deregisterContractTestSources

task contractTests(type: Test) {
    description = 'Runs contract tests'
    group = 'verification'

    testClassesDirs = sourceSets.contractTest.output.classesDirs
    classpath = sourceSets.contractTest.runtimeClasspath
}

contracts {
    baseClassForTests = 'com'
    generatedTestSourcesDir = file("${buildDir}/generated-contract-sources")
    generatedTestResourcesDir = file("${buildDir}/generated-contract-resources/")
    testFramework = "JUNIT5"
    contractsDslDir = new File("${projectDir}/src/contractTest/resources/contracts/")
    nameSuffixForTests = 'Spec'
    basePackageForTests = 'com'
}

like image 195
craigb Avatar answered Feb 19 '26 13:02

craigb



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!