Spring Cloud Contract org.junit.ComparisonFailure:expected:< [200]>但是:< [404]>在运行测试时

时间:2018-05-10 16:01:25

标签: spring-cloud-contract

我已经设置了一个示例项目来使用Spring Cloud Contract 2.0.0.RC1来实现它。当我运行测试时,我收到错误消息:

Tests run: 1, Failures: 1, Errors: 0, Skipped: 0, Time elapsed: 3.188 sec <<< FAILURE!
validate_shouldGetProfile(com.example.contractdemoserver.ContractdemoserverTest)  Time elapsed: 0.443 sec  <<< FAILURE!
org.junit.ComparisonFailure: expected:<[200]> but was:<[404]>
at com.example.contractdemoserver.ContractdemoserverTest.validate_shouldGetProfile(ContractdemoserverTest.java:27)

该项目建立并运行正常。我已经尝试了很多东西来解决这个测试错误,包括检查是否通过向Contractdemoserverbase.setup()(见下文)添加System.out.println()消息来调用ContractVerifieriBase,并且正在调用它。 / p>

我一直在尝试从这里跟踪/模拟生产者项目(运行和测试正常):

https://github.com/spring-cloud-samples/spring-cloud-contract-samples

我做错了什么?

项目结构的概念如下:

enter image description here

ContractdemoserverApplication:

package com.example.contractdemoserver;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class ContractdemoserverApplication {

    public static void main(String[] args) {
        SpringApplication.run(ContractdemoserverApplication.class, args);
    }
}

ServerDefaultController:

package com.example.contractdemoserver;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class ServerDefaultController {

    public static final String controllerName = "test";
    public static final String controllerPath = "/" + controllerName;

    @RequestMapping(value = controllerPath, headers={"application/json"})
    @ResponseBody
    public TestObject get(){
        return new TestObject(1001, new String[0]);
    }
}

的TestObject:

package com.example.contractdemoserver;

public class TestObject {

    private int id;
    private String[] cards;

    public TestObject(){}

    public TestObject(int id, String[] cards){
        setId(id);
        setCards(cards);
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String[] getCards() {
        return cards;
    }

    public void setCards(String[] cards) {
        this.cards = cards;
    }
}

ContractdemoserverBase:

package com.example.contractdemoserver;

import io.restassured.module.mockmvc.RestAssuredMockMvc;
import org.junit.Before;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
public abstract class ContractdemoserverBase {

    @Before
    public void setup() {
        RestAssuredMockMvc.standaloneSetup(new ServerDefaultController());
    }

}

shouldGetProfile.groovy:

package contracts.contractdemoserver

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

Contract.make {
    description("""
Represents a successful scenario of getting a response from the /test endpoint.

```
given:
    nothing
when:
    a request for a Test is made
then:
    returns a test object with id 1001 and no cards
```

""")
    request {
        method 'GET'
        url '/test'
        headers {
            contentType(applicationJson())
        }
    }
    response {
        status 200
        body("""
            {
                "id":1001,
                "cards":[]
            }
        """)
        headers {
            contentType(applicationJson())
        }
    }
}

的pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>contractdemoserver</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>server</name>
    <description>POC Server for Service Contract Testing</description>

    <properties>
        <docker-maven-plugin-version>0.25.0</docker-maven-plugin-version>
        <java.version>1.8</java.version>
        <maven-resources-plugin.version>3.0.2</maven-resources-plugin.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <spring-boot-dependencies-version>2.0.1.RELEASE</spring-boot-dependencies-version>
        <spring-cloud.version>Finchley.BUILD-SNAPSHOT</spring-cloud.version>
        <spring-cloud-contract.version>2.0.0.BUILD-SNAPSHOT</spring-cloud-contract.version>
    </properties>

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

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

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${spring-boot-dependencies-version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>2.0.2.RELEASE</version>
            </plugin>
            <plugin>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-contract-maven-plugin</artifactId>
                <version>${spring-cloud-contract.version}</version>
                <extensions>true</extensions>
                <configuration>
                    <!-- <baseClassForTests>com.example.contractdemoserver.ContractVerifierBase</baseClassForTests> -->
                    <packageWithBaseClasses>com.example.contractdemoserver</packageWithBaseClasses>
                </configuration>
            </plugin>
        </plugins>
    </build>


    <repositories>
        <repository>
            <id>spring-snapshots</id>
            <name>Spring Snapshots</name>
            <url>http://repo.spring.io/libs-snapshot-local</url>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </repository>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>http://repo.spring.io/libs-milestone-local</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
        <repository>
            <id>spring-releases</id>
            <name>Spring Releases</name>
            <url>http://repo.spring.io/release</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>
    <pluginRepositories>
        <pluginRepository>
            <id>spring-snapshots</id>
            <name>Spring Snapshots</name>
            <url>http://repo.spring.io/libs-snapshot-local</url>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </pluginRepository>
        <pluginRepository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>http://repo.spring.io/libs-milestone-local</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </pluginRepository>
        <pluginRepository>
            <id>spring-plugin-snapshots</id>
            <name>Spring Snapshots</name>
            <url>http://repo.spring.io/plugins-snapshot-local</url>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </pluginRepository>
        <pluginRepository>
            <id>spring-plugin-milestones</id>
            <name>Spring Milestones</name>
            <url>http://repo.spring.io/plugins-release-local</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </pluginRepository>
    </pluginRepositories>

</project>

任何建议都将不胜感激。

0 个答案:

没有答案