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

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

我有以下 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()')
    }
}

我在响应中调用 getResponseForFindTeamRolesByFilters() 以便从服务器生成动态响应。例如,原因可能是来自数据库的自动生成的 ID。 getResponseForFindTeamRolesByFilters() 生成的字符串是一个有效的 JSON,不幸的是,它被忽略了,并且 returns 在测试 运行.[=14 时始终为真=]

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

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

在这种情况下,响应被正确验证,如果不匹配则测试失败。 测试生成的代码证实了我所说的,如下所示:

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

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

如您所见,没有断言。它只是调用生成 json.

的方法

我应该如何让测试检查动态 json 响应? 谢谢!

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

你不应该这样做。合同测试不应访问数据库。

对于消费者你应该做的

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