From d8d5950e99dc94e653f8775e3ad9fa7af883f22e Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Thu, 15 Dec 2016 16:55:34 +0100 Subject: [PATCH] Enable validation in `AutoConfigureWebMvc` Closes gh-7582 --- .../ValidationAutoConfiguration.java | 2 +- spring-boot-test-autoconfigure/pom.xml | 10 +++++ .../main/resources/META-INF/spring.factories | 1 + .../web/servlet/ExampleController3.java | 41 +++++++++++++++++++ ...MvcTestAllControllersIntegrationTests.java | 22 ++++++++++ 5 files changed, 75 insertions(+), 1 deletion(-) create mode 100644 spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/web/servlet/ExampleController3.java diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/validation/ValidationAutoConfiguration.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/validation/ValidationAutoConfiguration.java index 8655122da1c..37e9713c802 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/validation/ValidationAutoConfiguration.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/validation/ValidationAutoConfiguration.java @@ -45,7 +45,7 @@ import org.springframework.validation.beanvalidation.MethodValidationPostProcess public class ValidationAutoConfiguration { @Bean - @ConditionalOnResource(resources = "META-INF/services/javax.validation.spi.ValidationProvider") + @ConditionalOnResource(resources = "classpath:META-INF/services/javax.validation.spi.ValidationProvider") @Conditional(OnValidatorAvailableCondition.class) @ConditionalOnMissingBean public MethodValidationPostProcessor methodValidationPostProcessor() { diff --git a/spring-boot-test-autoconfigure/pom.xml b/spring-boot-test-autoconfigure/pom.xml index d543c50b478..2fd8ee33312 100644 --- a/spring-boot-test-autoconfigure/pom.xml +++ b/spring-boot-test-autoconfigure/pom.xml @@ -173,5 +173,15 @@ spring-plugin-core test + + org.hibernate + hibernate-validator + test + + + org.apache.tomcat.embed + tomcat-embed-el + test + diff --git a/spring-boot-test-autoconfigure/src/main/resources/META-INF/spring.factories b/spring-boot-test-autoconfigure/src/main/resources/META-INF/spring.factories index ed62fd45e4f..8484d9b3f68 100644 --- a/spring-boot-test-autoconfigure/src/main/resources/META-INF/spring.factories +++ b/spring-boot-test-autoconfigure/src/main/resources/META-INF/spring.factories @@ -78,6 +78,7 @@ org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration,\ org.springframework.boot.autoconfigure.MessageSourceAutoConfiguration,\ org.springframework.boot.autoconfigure.mustache.MustacheAutoConfiguration,\ org.springframework.boot.autoconfigure.thymeleaf.ThymeleafAutoConfiguration,\ +org.springframework.boot.autoconfigure.validation.ValidationAutoConfiguration,\ org.springframework.boot.autoconfigure.web.HttpMessageConvertersAutoConfiguration,\ org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration diff --git a/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/web/servlet/ExampleController3.java b/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/web/servlet/ExampleController3.java new file mode 100644 index 00000000000..bdf6fe2183a --- /dev/null +++ b/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/web/servlet/ExampleController3.java @@ -0,0 +1,41 @@ +/* + * Copyright 2012-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.test.autoconfigure.web.servlet; + +import javax.validation.constraints.Size; + +import org.springframework.stereotype.Controller; +import org.springframework.validation.annotation.Validated; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RestController; + +/** + * Example {@link Controller} used with {@link WebMvcTest} tests. + * + * @author Stephane Nicoll + */ +@RestController +@Validated +public class ExampleController3 { + + @GetMapping("/three/{id}") + public String three(@PathVariable @Size(max = 4) String id) { + return "Hello " + id; + } + +} diff --git a/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/web/servlet/WebMvcTestAllControllersIntegrationTests.java b/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/web/servlet/WebMvcTestAllControllersIntegrationTests.java index 1e8cefddd79..5ea2c26a13f 100644 --- a/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/web/servlet/WebMvcTestAllControllersIntegrationTests.java +++ b/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/web/servlet/WebMvcTestAllControllersIntegrationTests.java @@ -16,13 +16,18 @@ package org.springframework.boot.test.autoconfigure.web.servlet; +import javax.validation.ConstraintViolationException; + +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.web.servlet.MockMvc; +import static org.hamcrest.CoreMatchers.isA; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; @@ -31,11 +36,15 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers. * Tests for {@link WebMvcTest} when no explicit controller is defined. * * @author Phillip Webb + * @author Stephane Nicoll */ @RunWith(SpringRunner.class) @WebMvcTest(secure = false) public class WebMvcTestAllControllersIntegrationTests { + @Rule + public ExpectedException thrown = ExpectedException.none(); + @Autowired private MockMvc mvc; @@ -57,4 +66,17 @@ public class WebMvcTestAllControllersIntegrationTests { .andExpect(status().isOk()); } + @Test + public void shouldRunValidationSuccess() throws Exception { + this.mvc.perform(get("/three/OK")) + .andExpect(status().isOk()) + .andExpect(content().string("Hello OK")); + } + + @Test + public void shouldRunValidationFailure() throws Exception { + this.thrown.expectCause(isA(ConstraintViolationException.class)); + this.mvc.perform(get("/three/invalid")); + } + }