kotlin - 春季测试 Kotlin Hamescreat 有匹配器
问题描述
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 断言?
解决方案
正如@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")))
推荐阅读
- node.js - 是否可以将 express js 与 MS Access 数据库一起使用?
- reactjs - TypeError: instance.render is not a function 编译成功后
- python - 数据框中的年初至今平均值
- excel - 如何将整个工作表复制到引用 Excel 中工作表名称的新创建工作表?
- linux - 仅用 awk 替换 sed/tail/grep
- node.js - 如何使用 AWS Lambda 生成带有自定义字体的 PDF?
- javascript - wix/react-native-navigation V2 | 第二个屏幕中的 setRoot bottomTabs
- java - 如何从 JSON 文档中替换反斜杠(“\\\\”)?
- c# - 如何通过 iText7 在 calibri 上设置字体?
- tsql - 将存储过程中的不同查询结果连接到一个数据集中