Spring Cloud Contract,主体和执行方法未验证响应 [英] Spring Cloud Contract, body and execute method is not validating the response

查看:75
本文介绍了Spring Cloud Contract,主体和执行方法未验证响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下春季云合同:

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

我在响应处调用 getResponseForFindTeamRolesByFilters(),以便从服务器生成动态响应.例如,原因可能是来自数据库的自动生成的ID.从 getResponseForFindTeamRolesByFilters()生成的字符串是有效的JSON,很遗憾,该字符串会被忽略,并且在测试运行时始终返回true.

I call the getResponseForFindTeamRolesByFilters() at the response in order to generate a dynamic response from the server. The reason could for example be an auto generated id that is coming from the DB. The generated string from the getResponseForFindTeamRolesByFilters() is a valid JSON that unfortunately is ignored and returns always true when the test run.

当我用如下静态响应替换execute方法时,我注意到了这一点:

I have noticed this when I replace the execute method with a static response like the following one:

"""
{
   "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();

您可以看到没有断言.它只是调用生成json的方法.

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? Thank you!

推荐答案

我在响应处调用getResponseForFindTeamRolesByFilters()以便从服务器生成动态响应.

I call the getResponseForFindTeamRolesByFilters() at the response in order to generate a dynamic response from the server.

您不应该这样做.合同测试不应访问数据库.

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"))
    }

对于制片人

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

这篇关于Spring Cloud Contract,主体和执行方法未验证响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆