使用 Kotlin 和 JUnit 5 测试 rest api
Testing rest api with Kotlin and JUnit 5
我正在休息时做睾丸 API,但我无法测试控制器的 "insert"。
我的控制器
@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"
我想你是想在 ObjectMapper
行上写 content
而不是 contentType
。
我正在休息时做睾丸 API,但我无法测试控制器的 "insert"。
我的控制器
@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"
我想你是想在 ObjectMapper
行上写 content
而不是 contentType
。