简体   繁体   English

Spring 云合同、正文和执行方法未验证响应

[英]Spring Cloud Contract, body and execute method is not validating the response

I have the following spring cloud contract:我有以下 spring 云合同:

package contracts.teams

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

Contract.make {
    name"d Find team roles by filters"
    description "Find team roles by filters"

    request {
        method "POST"
        url "api/team/findTeamRolesByFilters"
        headers {
            contentType applicationJson()
            accept applicationJson()
            header"Authorization", execute('bearerOfAccessToken()')
        }
        body execute('getRequestForFindTeamRolesByFilters()')
    }

    response {
        status OK()
        headers {
            contentType applicationJson()
        }
        body execute('getResponseForFindTeamRolesByFilters()')
    }
}

I call the getResponseForFindTeamRolesByFilters() at the response in order to generate a dynamic response from the server.我在响应中调用getResponseForFindTeamRolesByFilters()以便从服务器生成动态响应。 The reason could for example be an auto generated id that is coming from the DB.例如,原因可能是来自数据库的自动生成的 id。 The generated string from the getResponseForFindTeamRolesByFilters() is a valid JSON that unfortunately is ignored and returns always true when the test run.getResponseForFindTeamRolesByFilters()生成的字符串是一个有效的 JSON,不幸的是它被忽略并在测试运行时始终返回 true。

I have noticed this when I replace the execute method with a static response like the following one:当我用 static 响应替换执行方法时,我注意到了这一点,如下所示:

"""
{
   "success": "false"
}
"""

In this case the response is being validated correctly and fails the test in case it does not match.在这种情况下,响应被正确验证,如果不匹配,则测试失败。 What I said is being confirmed by the test generated code as it can be seen here:我所说的得到了测试生成的代码的确认,如下所示:

// then:
            assertThat(response.statusCode()).isEqualTo(200);
            assertThat(response.header("Content-Type")).matches("application/json.*");

        // and:
            DocumentContext parsedJson = JsonPath.parse(response.getBody().asString());
            getResponseForFindTeamRolesByFilters();

As you can see there is no assertion.如您所见,没有断言。 It simply calls the method that generates the json.它只是调用生成 json 的方法。

How am I supposed to make the test check the dynamic json response?我应该如何进行测试检查动态 json 响应? Thank you!谢谢!

I call the getResponseForFindTeamRolesByFilters() at the response in order to generate a dynamic response from the server.我在响应中调用 getResponseForFindTeamRolesByFilters() 以便从服务器生成动态响应。

You should not do it.你不应该这样做。 Contract tests should not access the database.合同测试不应访问数据库。

For the consumer you should do对于消费者你应该做

request {
        method "POST"
        url "api/team/findTeamRolesByFilters"
        headers {
            contentType applicationJson()
            accept applicationJson()
            header"Authorization", execute('bearerOfAccessToken()')
        }
        body $(producer(execute('getRequestForFindTeamRolesByFilters()')), consumer("some value to be put on the consumer side"))
    }

For the producer对于生产者

response {
    status OK()
    headers {
        contentType applicationJson()
    }
    body $(producer(execute('getResponseForFindTeamRolesByFilters()')), consumer("something in the stub"))
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM