Philipp Mayer
11/27/2020, 8:12 PM@Serializable
class RouteDto(
val id: Int? = null,
val template: Template?,
val server: Server?,
val variables: List<Variable>,
val name: String,
val active: Boolean,
val profile: Profile,
val description: String
)
And I have the following endpoint:
@RestController
@RequestMapping("/routes")
class RouteController(
@PostMapping("/new")
fun createRoute(routeDto: RouteDto): ResponseEntity<RouteDto?> {
val route = saveRouteService.saveRouteFrom(routeDto)
val dto: RouteDto = route.id?.toDto() ?: return ResponseEntity.unprocessableEntity().build()
return ResponseEntity.ok(dto)
}
Now I just wanted to call it like that with mockMvc:
@Test
fun `should create a route`() {
val dto = RouteDto(null, null, null, listOf(),"testDto", true, Profile.DEV, "should create a route test")
<http://mockMvc.post|mockMvc.post>("/routes/new") {
contentType = MediaType.APPLICATION_JSON
content = Json.encodeToString(dto)
}.andExpect {
status { is2xxSuccessful() }
}
}
However, I get the following Warning:
DefaultHandlerExceptionResolver : Resolved [org.springframework.validation.BindException: org.springframework.validation.BeanPropertyBindingResult: 1 errors
Field error in object 'routeDto' on field 'active': rejected value [null]; codes [typeMismatch.routeDto.active,typeMismatch.active,typeMismatch.boolean,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [routeDto.active,active]; arguments []; default message [active]]; default message [Failed to convert value of type 'null' to required type 'boolean'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [null] to type [boolean] for value 'null'; nested exception is java.lang.IllegalArgumentException: A null value cannot be assigned to a primitive type]]
Spring seems to fail at reading the Boolean value active
, which is actually correctly serialized:
MockHttpServletRequest:
HTTP Method = POST
Request URI = /routes/new
Parameters = {}
Headers = [Content-Type:"application/json;charset=UTF-8", Content-Length:"136"]
Body = {"template":null,"server":null,"variables":[],"name":"testDto","active":true,"profile":"DEV","description":"should create a route test"}
Session Attrs = {}
It would be great if someone could point me in a helpful direction. Thanks in advance!kqr
11/27/2020, 8:25 PMJukka Siivonen
11/27/2020, 8:42 PMmickeelm
11/27/2020, 8:51 PMactive
from the mix here and see if that solves the problem or if that's just the first one it hits.mickeelm
11/27/2020, 9:04 PMPhilipp Mayer
11/27/2020, 11:34 PMkqr
11/28/2020, 1:12 PMPhilipp Mayer
11/28/2020, 2:34 PM