Home > Software design >  how to perform the bean validation through groovy unit test
how to perform the bean validation through groovy unit test

Time:08-11

I am trying to perform some parameters validations on the interface which is implements by the controller class.

interface (generated using swagger)

@Validated
@Tag(name = "PersonService", description = "the person API")
public interface PersonServiceApi {

 @RequestMapping(
    method = RequestMethod.GET,
    value = "/person",
    produces = { "application/json" }
)
Mono<ResponseEntity<Person>> searchPerson(
    @NotNull @Pattern(regexp = "[A-Za-z0-9-] ") @Size(min = 1, max = 30) @Parameter(name = "name", required = true) @Valid @RequestParam(value = "name", required = true) String name,
    @Pattern(regexp = "^[A-Za-z0-9] ") @Size(min = 0, max = 9) @Parameter(name = "id") @Valid @RequestParam(value = "id", required = false) String id,
    @Min(0) @Parameter(name = "offset") @Valid @RequestParam(value = "offset", required = false, defaultValue = "0") Integer offset,
    @Min(1) @Max(30) @Parameter(name = "limit") @Valid @RequestParam(value = "limit", required = false, defaultValue = "30") Integer limit,
    @Parameter(hidden = true) final ServerWebExchange exchange
);

}

Implementation controller:

 @RestController


@Slf4j
public class PesrsonController implements PersonServiceApi {
@Override
    public Mono<ResponseEntity<Person>> searchPerson(String name, String id, Integer offset, Integer limit,
                                                                     ServerWebExchange exchange) {
        return this.personService.searchperson(name, id, offset, limit)
                .subscribeOn(Schedulers.boundedElastic())
                .map(ResponseEntity::ok)
                .switchIfEmpty(Mono.just(ResponseEntity.noContent().build()));
    }
}

Groovy test:

  @Unroll
        def '"test person name with null "'() {
           
            given: 'service return person details'
            personService.searchperson(name, id, offset, limit) >> Mono.just(new Person())
            when: 'searchpseron by name'
            def response =personControllerTest.searchPerson(name, id, offset, limit).block()
    
            then:
            ConstraintViolationException
    
            where:
    
            name   | id    | offset | limit  |  expectedStatus
            ""     | "750" |   0    |   30   |  HttpStatus.BAD_REQUEST

}

I want to validate @NotNull, I am expecting ConstraintViolationException while passing the empty or null for name.

not sure am following the Groovy test propery or i should follow something with ValidatorFactory? Please guide me here

CodePudding user response:

Interface @Validated should work - https://github.com/spring-projects/spring-boot/issues/17000 . Most probably your controller return BadRequest. Please keep in mind that RestControllers throw a MethodArgumentNotValidException not ConstraintViolationException.

I'm not sure what exactly happened on your case, because response/stacktrace is missing, but my advice here is to debug it.

Easies way for this is to add an Advicer and see the exception which is coming:

@ControllerAdvice
public class MyAdvicer


@ExceptionHandler(value = Exception.class)
public ResponseEntity<Object> handleException(Exception exception) {
    return debug it here!!!
}

CodePudding user response:

ReservationManagement object = new ReservationManagement();
Method method = ReservationManagement.class
  .getMethod("createReservation", LocalDate.class, int.class, Customer.class);
Object[] parameterValues = { LocalDate.now(), 0, null };
Set<ConstraintViolation<ReservationManagement>> violations 
  = executableValidator.validateParameters(object, method, parameterValues);

https://www.baeldung.com/javax-validation-method-constraints#3-programmatic-validation

  • Related