rest - 使用 Kotlin 和 JUnit 5 测试 rest api
问题描述
我正在从我的 Rest API 做睾丸,但我无法测试控制器的“插入”。
我的控制器
@PostMapping(value = ["/api/clients/lp"])
override fun insert(@Valid @RequestBody model: LegalPersonModel): ResponseEntity<Any> {
service.insert(service.modelToEntity(model))
return ResponseEntity.ok().body(HttpStatus.CREATED)
}
我的测试
val lp = LegalPerson(1L, 1L, true, "Test Fantasy", "Test LTDA",
"test@com", "40.492.967/0001-52", "Test S", "Test M",
LocalDate.of(2017, 10, 21), 1L, listOf(1L, 22L))
@Test
fun insert() {
Mockito.doReturn(lp).`when`(service)!!.insert(lp)
mockMvc?.perform(MockMvcRequestBuilders.post("/api/clients/lp")
.contentType(MediaType.APPLICATION_JSON_UTF8)
.contentType(ObjectMapper().registerModule(JavaTimeModule()).let {
it.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
it.writeValueAsString(lp)
}))
?.andExpect(MockMvcResultMatchers.status().isCreated)
?.andExpect(MockMvcResultMatchers.content().contentType(MediaType.APPLICATION_JSON_UTF8_VALUE))
}
错误:
org.springframework.http.InvalidMediaTypeException: Invalid mime type "{"id":1,"companyId":1,"active":true,"tradeName":"Test Fantasy","companyName":"Test LTDA","email":"test@com","cnpj":"40.492.967/0001-52","stateRegistration":"Test S","municipalRegistration":"Test M","openingDate":"2017-10-21","address":1,"phones":[1,22]}": Invalid token character '{' in token "{"id":1,"companyId":1,"active":true,"tradeName":"Test Fantasy","companyName":"Test LTDA","email":"test@com","cnpj":"40.492.967"
解决方案
我认为您的意思是写content
而不是contentType
与ObjectMapper
.
推荐阅读
- java - 如果指定的内存丢失,jvm 参数 xms 是否意味着进程不会启动?
- google-sheets - 如何将自定义格式规则中的这些数组公式和公式组合在自定义格式规则中的 1 个公式中?
- javascript - asp.net MVC 中嵌套表视图中级别 3 子子项的问题
- java - Spring Aws SQS 未连接到 localstack
- python - 解析yaml列表并将其保存到python中的变量
- python-requests - 使用 Token 处理 requests.get
- javascript - 从 JS 中的多个对象数组中获取唯一值 - javascript 中的正确方法
- python - 递归到字典深度
- r - R + rvest + 礼貌用于网络抓取 etsy.com
- xml - Groovy SAP CPI 中的日期比较