Reputation: 1828
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
}
Upvotes: 0
Views: 112
Reputation: 45
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
Upvotes: 1