Spring Cloud Contract org.junit.ComparisonFailure:预期:<[200]>,但在运行测试时为:<[401]>

时间:2018-07-29 04:39:26

标签: spring gradle

我需要将基于Maven的Spring Cloud Contract 2.0.0.RELEASE依赖项转换为基于Gradle的程序。这是pom.xml。

<properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <spring-cloud-contract.version>2.0.0.RELEASE</spring-cloud-contract.version>
    </properties>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Edgware.SR4</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>       
        </dependencies>
    </dependencyManagement>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-webflux</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-contract-verifier</artifactId>
            <scope>test</scope>
        </dependency>   

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>io.projectreactor</groupId>
            <artifactId>reactor-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>           

            <plugin>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-contract-maven-plugin</artifactId>
                <version>${spring-cloud-contract.version}</version>
                <extensions>true</extensions>
                <configuration>
                    <packageWithBaseClasses>com.globallogic.springfluxdemo.cdc</packageWithBaseClasses>
                    <testMode>EXPLICIT</testMode>
                </configuration>
            </plugin>
        </plugins>
    </build>

但是使用gradle init我已经将maven项目转换为gradle一个。但是,效果不佳。后来我在build.gradle中添加了合约插件。这是build.gradle。

apply plugin: 'io.spring.dependency-management'
apply plugin: 'spring-cloud-contract'


dependencyManagement {
    imports {
         mavenBom 'org.springframework.cloud:spring-cloud-dependencies:Edgware.SR4'
    }
}


contracts {
      testMode = "EXPLICIT"
      baseClassForTests = 'com.startwithjava.springwebfluxcontract.cdc'
    }
}

合同常规dsl

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

Contract.make {
  description "Get Person Object"
  request {
    method GET()
    url value("/mono")
    headers {
      contentType(applicationJson())
      header('''Accept''', '''application/json''')
    }
  }
  response {
    status 200
    bodyMatchers{
      jsonPath('$.id', byType())
      jsonPath('$.name', byType())

    }
    headers { contentType(applicationJson()) }
    body (''' {
    "id": 1,
    "name": "Raj"
}''')
  }
}

但是在构建gradle时遇到问题,它说返回401状态而不是200。

1 个答案:

答案 0 :(得分:0)

根据合同,baseClassForTests的配置有些错误。我放置了包而不是类名。

contracts{
    testMode = 'EXPLICIT'
    baseClassForTests = 'com.startwithjava.springwebfluxcontract.cdc.PersonRestBase'
}