Skip to content

Test generation in "spring-cloud-contract-maven-plugin" fails when there are multiple <execution> sections with goal "generateTests". #1807

@valerii-yakubovych

Description

@valerii-yakubovych

Context:

  • There is dedicated repository with multiple consumer contracts that are released as different Maven artifacts, i.e. 'some-stubs-1', 'some-stubs-2', and so on.
  • There is a service with two producers, producer-1 and producer-2.
  • Producer-1 should be tested against 'some-stubs-1' contract.
  • Producer-2 should be tested against 'some-stubs-2' contract.
  • There is spring-cloud-contract-maven-plugin:3.1.3 plugin with two <execution> sections. Section with id execution-1 is supposed to execute tests against producer-1. Section with id execution-2 is supposed to execute tests against producer-2. Two <execution> sections are necessary because it is not possible to put more then one <contractDependency> in one <configuration>.
<plugin>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-contract-maven-plugin</artifactId>
    <version>3.1.3</version>
    <executions>
        <execution>
            <id>execution-1</id>
            <goals>
                <goal>generateTests</goal>
            <goals>
            <configuration>
                <contractDependency>
                    <groupId>com.experiment</groupId>
                    <artifactId>some-stubs-1</artifactId>
                    <version>1.0.0</version>
                    <classifier>stubs</classifier>
                <contractDependency>
                <packageWithBaseClasses>package.base.stubs1</packageWithBaseClasses>
            <configuration>
        </execution>
        <execution>
            <id>execution-2</id>
            <goals>
                <goal>generateTests</goal>
            <goals>
            <configuration>
                <contractDependency>
                    <groupId>com.experiment</groupId>
                    <artifactId>some-stubs-2</artifactId>
                    <version>1.0.0</version>
                    <classifier>stubs</classifier>
                <contractDependency>
                <packageWithBaseClasses>package.base.stubs2</packageWithBaseClasses>
            <configuration>
        </execution>
    <executions>
    <configuration>
        <nameSuffixForTests>CdcTest</nameSuffixForTests>
        <contractsPath>/</contractsPath>
        <contractsMode>LOCAL</contractsMode>
    <configuration>
<plugin>

some-stubs-1 contract:

package com.example

import org.springframework.cloud.contract.spec.Contract

Contract.make {
  label("coolTest")
  input {
    triggeredBy("coolTest()")
  }
  outputMessage {
    sentTo("cool.kafka.topic")
    body'''
      {
        "cool": 1234
      }
    '''
  }
}

some-stubs-2 contract:

package com.example

import org.springframework.cloud.contract.spec.Contract

Contract.make {
  label("nastyTest")
  input {
    triggeredBy("nastyTest()")
  }
  outputMessage {
    sentTo("nasty.kafka.topic")
    body'''
      {
        "nasty": 1234
      }
    '''
  }
}

mvn clean install
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.7.0:testCompile (default-testCompile) on project cool-project: Compilation failure
[ERROR] .../target/generated-test-sources/contracts/.../some-stubs-1/CoolCdcTest.java:[20,8] duplicate class: com.example.cdc.META_INF.com.example.some-stubs-2...CoolCdcTest

What actually happens:
Plugin generates two test classes with the same name in different packages:

com.example.META_INF...some-stubs-1...CoolCdcTest
com.example.META_INF...some-stubs-2...CoolCdcTest

Why it happens:
Two artifacts some-stubs-1 and some-stubs-2 should be download. But MavenContractsDownloader class after downloading the first some-stubs-1 artifact caches it.
And before downloading the second artifact some-stubs-2, MavenContractsDownloader checks the cache and returns first cached artifact some-stubs-1 as the second some-stubs-2.
I.e. caching does not check is it correct artifact cached or not. It just check that something is cached and returns it.

It seems there may be two solutions:

  • cache artifact's groupId:artifactId:version and check against it
  • make cache deactivatable with some property

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions