0

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
}
CRISTIAN ROMERO MATESANZ
  • 1,502
  • 3
  • 14
  • 26

1 Answers1

1

Validation on Base should be @field:Valid val base: Base instead of @Valid val base: Base

field: specifies the annotation is applied on the field not construc

ref:

https://stackoverflow.com/a/35853200

https://stackoverflow.com/a/36521309

Zii
  • 45
  • 2
  • 10