小编Eri*_*ric的帖子

Spring Cloud 合同:生成的测试没有配置 MockMVC 并且编译失败

我遇到了“您尚未配置 MockMVC 实例”的问题。“mvn全新安装”时的异常。

跑步

org.springframework.cloud.contract.verifier.tests.ContractVerifierTest 测试运行:1,失败:0,错误:1,跳过:0,时间:0.185 秒 <<< 失败!- 在 org.springframework.cloud.contract.verifier.tests.ContractVerifierTest validate_shouldGetAmenities(org.springframework.cloud.contract.verifier.tests.ContractVerifierTest) 已用时间:0.184 秒 <<< 错误!java.lang.IllegalStateException:您尚未配置 MockMVC 实例。你可以静态地做到这一点

或使用 DSL:

给定()。模拟Mvc(..)。..

问题是,抛出此异常的测试是基于合约生成的测试。

这是合同。

package contracts

org.springframework.cloud.contract.spec.Contract.make {
    request {
        method 'GET'
        url '/abc/def/serviceA?catalog=x'
        body("")
    }
    response {
        status 200
        body(""
        )
        headers {
            contentType(applicationJsonUtf8())
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

这是生成的测试。

package org.springframework.cloud.contract.verifier.tests;

public class ContractVerifierTest {

@Test
public void validate_shouldGetMyStuff() throws Exception {
        // given:
        MockMvcRequestSpecification request = given()
                .body("\"\"");

        // when:
            ResponseOptions response = given().spec(request)
                .get("/abc/def/serviceA?catalog=x");

        // then:
            assertThat(response.statusCode()).isEqualTo(200);
            assertThat(response.header("Content-Type")).matches("application/json;charset=UTF-8.*");
        // …
Run Code Online (Sandbox Code Playgroud)

spring-cloud spring-cloud-contract

7
推荐指数
1
解决办法
5069
查看次数