【问题标题】:Spring Test Kotlin Hamescreat has matcher春季测试 Kotlin Hamescreat 有匹配器
【发布时间】:2021-10-02 17:14:48
【问题描述】:

java中的jsonPath断言:

mockMvc.perform(post("/books")
                .content(bookInJson)
                .header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON))
                .andDo(print())
                .andExpect(status().isBadRequest())
                .andExpect(jsonPath("$.timestamp", is(notNullValue())))
                .andExpect(jsonPath("$.status", is(400)))
                .andExpect(jsonPath("$.errors").isArray())
                .andExpect(jsonPath("$.errors", hasSize(3)))
                .andExpect(jsonPath("$.errors", hasItem("Author is not allowed.")))

无法在 kotlin 等效项中编译:

mockMvc!!.perform(get("/api/customer/{id}", customer.id))
      .andExpect(status().isOk)
      .andExpect(jsonPath("$.name", equalTo(customer.name)))
      .andExpect(jsonPath("$.idNumber", equalTo(customer.idNumber)))
      .andExpect(jsonPath("$.address", hasSize(0)))
      .andExpect(jsonPath("$.contact", hasSize(0)))
      .andExpect(jsonPath("$.address", hasItem("item")))

hasSize 断言抛出错误:Kotlin: Not enough information to infer type variable T

如何使用 kotlin 在 jsonPath 中执行 hasSize 断言?

【问题讨论】:

  • 您是否尝试过指定类型?例如hasSize<String>(0)

标签: kotlin spring-test


【解决方案1】:

正如@F43nd1r 所说,指定类型有效。

代码:

mockMvc!!.perform(get("/api/customer/{id}", customer.id))
      .andExpect(status().isOk)
      .andExpect(jsonPath("$.name", equalTo(customer.name)))
      .andExpect(jsonPath("$.idNumber", equalTo(customer.idNumber)))
      .andExpect(jsonPath("$.address", hasSize<String>(0)))
      .andExpect(jsonPath("$.contact", hasSize<String>(0)))
      .andExpect(jsonPath("$.address", hasItem("item")))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-09-26
    • 2012-09-12
    • 1970-01-01
    • 1970-01-01
    • 2019-08-02
    • 2020-05-18
    • 2022-10-14
    • 1970-01-01
    相关资源
    最近更新 更多