I need to create a recursive validation to be able to validate PizzaCreate Object and base attribute in recursive mode using Kotlin. The test should return 400 but it returns 200 ok (Base name size must be greater than 2):
data class Base(@field:Length(min = 2, max = 100) val name:String)
data class PizzaCreate(
val id: Long,
@field:Length(min = 2, max = 100) val name: String,
val description: String,
val price: Int,
@Valid val base: Base
)
@RestController
@RequestMapping("/pizza")
class PizzaController(val pizzaService: PizzaService) {
@PostMapping
fun post(@RequestBody @Valid pizza: PizzaCreate) = pizzaService.addPizza(pizza)
}
@Test
fun `should add pizza `() {
val pizza = easyRandom.nextObject(PizzaCreate::class.java).copy(id = 1, name="aaa", base = Base(""))
val pizzaOut = PizzaOut(id=1,name=pizza.name,description = pizza.description,price = pizza.price)
`when`(pizzaService.addPizza(pizza)).thenReturn(pizzaOut.toMono())
webTestClient.post()
.uri("/pizza")
.bodyValue(pizza)
.exchange()
.expectStatus().isBadRequest
.returnResult<PizzaOut>().responseBody
}