-
Notifications
You must be signed in to change notification settings - Fork 38.5k
Inconsistent validation of query parameters when using MockMvc #24000
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Can you please provide the code for the mapped controller method that you are testing? |
@sbrannen here is a stripped down example of the controller... @RestController
@Validated
public class MyController {
@GetMapping("/my-endpoint")
public AResponse myEndpoint(
@RequestParam
@Pattern(regexp = "\\d+", message = "Invalid param, must be number")
String param)
{...}
} |
What happens if you move the @RestController
public class MyController {
@GetMapping("/my-endpoint")
public AResponse myEndpoint(
@Validated
@RequestParam
@Pattern(regexp = "\\d+", message = "Invalid param, must be number")
String param)
{...}
} |
@sbrannen, in that case the validation does not appear happen in any of the above examples. |
Thanks for trying that and reporting back. And... I apologize: that's not supposed to work. |
All of the examples you have supplied pass with proper validation for me, as can be seen in the following passing test class. import javax.validation.ConstraintViolationException;
import javax.validation.constraints.Pattern;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.context.junit.jupiter.web.SpringJUnitWebConfig;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.validation.annotation.Validated;
import org.springframework.validation.beanvalidation.MethodValidationPostProcessor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.util.NestedServletException;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.setup.MockMvcBuilders.webAppContextSetup;
@SpringJUnitWebConfig
class ValidatedControllerTests {
MockMvc mockMvc;
@BeforeEach
void setUp(WebApplicationContext wac) {
this.mockMvc = webAppContextSetup(wac).build();
}
@Test
void validationIsApplied() throws Exception {
assertThatExceptionOfType(NestedServletException.class)//
.isThrownBy(() -> mockMvc.perform(get("/my-endpoint?param={param}", "value")))//
.withCauseExactlyInstanceOf(ConstraintViolationException.class);
assertThatExceptionOfType(NestedServletException.class)//
.isThrownBy(() -> mockMvc.perform(get(String.format("/my-endpoint?param=%s", "value"))))//
.withCauseExactlyInstanceOf(ConstraintViolationException.class);
assertThatExceptionOfType(NestedServletException.class)//
.isThrownBy(() -> mockMvc.perform(get("/my-endpoint?param=%s", "value")))//
.withCauseExactlyInstanceOf(ConstraintViolationException.class);
assertThatExceptionOfType(NestedServletException.class)//
.isThrownBy(() -> mockMvc.perform(get("/my-endpoint?param={}", "value")))//
.withCauseExactlyInstanceOf(ConstraintViolationException.class);
}
@Configuration
@EnableWebMvc
static class WebConfig {
@Bean
MethodValidationPostProcessor methodValidationPostProcessor() {
return new MethodValidationPostProcessor();
}
@Bean
MyController myController() {
return new MyController();
}
}
@RestController
@Validated
static class MyController {
@GetMapping("/my-endpoint")
String myEndpoint(@RequestParam @Pattern(regexp = "\\d+", message = "Must be a number") String param) {
return "results";
}
}
} I am therefore closing this issue. |
Note, however, that |
Uh oh!
There was an error while loading. Please reload this page.
Affects:
spring-test:5.1.6
When using
MockMvc
to perform test requests of a@Validated
controller endpoint, validations (javax.validation.constraints) are not applied in certain cases depending on the formatting of the URI.Just raising this in case it can be made more consistent or if this might help someone else in the future.
The text was updated successfully, but these errors were encountered: