简体   繁体   English

使用spring-cloud-contract-oa3和spring cloud contract gradle插件时无法生成合约测试

[英]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 .当我在 OA3 yml 文件中指定合同然后构建项目时,构建通过但它不会生成合同测试。 If I replace the OA3 yml contract file with a groovy equivalent then the build passes and it generates and runs the contract tests.如果我将 OA3 yml 合同文件替换为 groovy 等效文件,则构建通过并生成并运行合同测试。

I have the following setup:我有以下设置:

  • com.github.mzielinski:spring-cloud-contract-oa3:3.1.2.0' com.github.mzielinski:spring-cloud-contract-oa3:3.1.2.0'
  • 'org.springframework.cloud:spring-cloud-contract-gradle-plugin:3.1.2' 'org.springframework.cloud:spring-cloud-contract-gradle-plugin:3.1.2'
  • 'org.springframework.cloud:spring-cloud-dependencies:2021.0.3' 'org.springframework.cloud:spring-cloud-dependencies:2021.0.3'
  • gradle 7.3.2毕业 7.3.2
  • java 8爪哇 8

my build.gradle looks like:我的 build.gradle 看起来像:

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'
}

}

@Lamminski, I guess, I was able to reproduce your issue. @Lamminski,我想,我能够重现您的问题。

First issue is with 'com.github.mzielinski:spring-cloud-contract-oa3:3.1.2.0' in buildSrc.第一个问题是 buildSrc 中的 'com.github.mzielinski:spring-cloud-contract-oa3:3.1.2.0'。 Please, move it into testImplementation.请把它移到 testImplementation 中。 Then you should be able to see generated contracts in /tmp/sccoa3___.yml files然后你应该能够在 /tmp/sccoa3___.yml 文件中看到生成的合约

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.一般来说,spring-cloud-contract-oa3 将 open-api 转换为 /tmp 目录下的 yaml 合约,然后这些文件用于从 SSC 生成具有 YmlContract 类的合约 DSL。

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):此外,我还注意到使用来自https://github.com/mzielinski/spring-cloud-contract-oa3/blob/develop/src/test/resources/openapi/verify_oa3.yml的示例和您的 build.gradle 文件, scc 在两个地方(到测试)正确转换从此文件生成的合同时存在问题:

  • message: 'Provided path in request is: {{{ jsonpath this "$.path" }}}.'消息:'请求中提供的路径是:{{{ jsonpath this "$.path" }}}。'
  • body: path: '/etc/passwd'正文:路径:'/etc/passwd'

In SPOCK for example generated test has such code在 SPOCK 例如生成的测试有这样的代码

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.我在生产中使用 sscoa3,我还没有遇到这样的问题,但我会尝试在 SSC 中修复 YamlContract 以正确转换它。

You are able to see stacktrace from YmlContract also by running gradle build with --debug flag.您还可以通过使用 --debug 标志运行 gradle build 从 YmlContract 看到堆栈跟踪。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Spring Cloud Contract 在合约文件中使用 bodyFromFile 生成弱测试 - Spring Cloud Contract generates weak tests using bodyFromFile in contract file 生成的测试在Spring Cloud Contract中失败 - Generated tests fail in Spring Cloud Contract Spring云合同和Gradle设置问题 - Spring Cloud Contract and Gradle Setup Question 如何在不构建项目中的所有单元和合同测试的情况下生成和运行单个合同测试? Java Spring Cloud 合约验证器 - How to generate and run a single contract test without building all the unit and contract tests in the project? Java Spring Cloud Contract Verifier Spring Cloud Contract 测试适用于 Maven,但不适用于 JUnit - Spring Cloud Contract tests work with Maven but not when run with JUnit 无法在Spring Cloud Contract中发送过帐请求 - Unable to send the post request in spring cloud contract 做spring cloud合约测试时找不到@SpringBootConfiguration - Unable to find a @SpringBootConfiguration when doing spring cloud contract test 如何在 spring-cloud-gateway 合约测试中从 spring-cloud-contract 中设置带有 StubRunner 端口的 url - How to set urls with port of StubRunner from spring-cloud-contract in spring-cloud-gateway contract tests Spring Cloud Contract和Webflux路由 - Spring cloud contract and webflux routing Spring Cloud Contract:多个控制器 - Spring Cloud Contract: Multiple controllers
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM