0

I have a @NotNull validation for some fields in a POJO which represents my request params:

Endpoint:

public ResponseEntity<Void> initializeLaunchPost(@Valid OidcFlowInitRequest request) throws LtiValidationException {

Request Body

@RequiredArgsConstructor
@ToString
@Getter
public class OidcFlowInitRequest { //java record public record keyword
    @NotNull
    private final String iss;
    @NotNull
    private final String loginHint;
    @NotNull
    private final String targetLinkUri;
}

I'm performing a test to check the exception thrown:

@Test
    void whenRequiredParamNotPassedReturnBadRequest() throws Exception {
        MultiValueMap<String, String> wrongInitFlowRequestParams = new LinkedMultiValueMap<>(initFlowRequestParams);
        wrongInitFlowRequestParams.remove("iss");

        mockMvc.perform(post(OIDC_INIT_FLOW_ENDPOINT).contentType(MediaType.APPLICATION_FORM_URLENCODED).params(wrongInitFlowRequestParams))
                .andExpect(status().isBadRequest()).andExpect(jsonPath("$.description", containsString("Mandatory request param")))
                .andDo(print());
    }

The problem is that the only way to execute my exception handler is overriding this method which is marked as deprecated and for removal:

@RestControllerAdvice
public class ControllerExceptionAdvice extends ResponseEntityExceptionHandler {

    @Override
    protected ResponseEntity<Object> handleBindException(BindException ex, HttpHeaders headers, HttpStatusCode status, WebRequest request) {
        String field = ex.getFieldError().getField();
        LtiErrorResponse body = new LtiErrorResponse(HttpStatus.BAD_REQUEST.value(),
                "Mandatory request param <" + field + "> not present");
        return handleExceptionInternal(ex, body, headers, status, request);
    }
}

According to some post the test should be throwing MethodArgumentNotValid but if I implement that method is not executed. In fact, my test prints this in the console:

Resolved Exception:
             Type = org.springframework.validation.BindException

So what other alternative do I have? My current solution it's not future proved.

0 Answers0