java - 如何使用 Spring Cloud Contracts 将 Stub 中的 NULL 值设置为 clientValue

标签 java groovy spring-cloud spring-cloud-contract

我们正在尝试将 null 值设置到生产者客户端站点代码中。

Groovy 合约示例:

Contract.make {
        description("Creating user")
        name("Create user")
        request {
            method 'POST'
            url '/api/user'
            body(
                    name: $(consumer(anyNonEmptyString()), producer('John Doe')),
                    address: $(consumer(optional(regex(alphaNumeric()))), producer(null))
            )
            headers {
                contentType(applicationJson())
            }
        }
        response {
            status 201
        }
    }

尝试解析 Groovy 合约时,以下合约产生断言失败:

assert testSide ==~ Pattern.compile(stubSide.optionalPattern())
   |        |           |       |        |
   null     false       |       |        ([a-zA-Z0-9]+)?
                        |       ([a-zA-Z0-9]+)?
                        ([a-zA-Z0-9]+)?

任何建议在生成的类中将其设置为 null 以获得这样的结果(或者干脆不添加字段 address:

@Test
public void create_user() throws Exception {
    // given:
        MockMvcRequestSpecification request = given()
                .header("Content-Type", "application/json")
                .body("{\"name\":\"John Doe\",\"address\": null,");

    // when:
        ResponseOptions response = given().spec(request)
                .post("/api/user");

    // then:
        assertThat(response.statusCode()).isEqualTo(201);
}

最佳答案

Spring Cloud Contract 中存在一个问题,我们将可选属性视为设置为空或非空的值,但它必须存在。固定https://github.com/spring-cloud/spring-cloud-contract/issues/1257我们还添加了对 null 值的支持。

关于java - 如何使用 Spring Cloud Contracts 将 Stub 中的 NULL 值设置为 clientValue,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58715263/

相关文章:

java - Spring启动问题-java.lang.IllegalArgumentException : Value must only contain valid chars

grails - 日期参数形式问题

apache-kafka - Spring Kafka 消费者无法消费记录

java - 为什么我的菜单栏不显示?

java - 如何有效地搜索返回数组列表中键的所有值

open-source - 我可以学习的 Great Grails 开源应用程序

具有 undefined variable 的 Jenkins 管道/Groovy 脚本

spring-boot - 无法构建 `reactor.core.publisher.Mono` Spring Cloud OpenFeign 和 Spring boot 2 的实例

java - Spring MVC 之前的 JSP 和 servlet

java - 如何在servlet中使用属性键值