Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in / Register
Toggle navigation
S
spring-boot
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
DEMO
spring-boot
Commits
d8d5950e
Commit
d8d5950e
authored
Dec 15, 2016
by
Stephane Nicoll
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Enable validation in `AutoConfigureWebMvc`
Closes gh-7582
parent
c2ca21bb
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
75 additions
and
1 deletion
+75
-1
ValidationAutoConfiguration.java
...autoconfigure/validation/ValidationAutoConfiguration.java
+1
-1
pom.xml
spring-boot-test-autoconfigure/pom.xml
+10
-0
spring.factories
...utoconfigure/src/main/resources/META-INF/spring.factories
+1
-0
ExampleController3.java
...ot/test/autoconfigure/web/servlet/ExampleController3.java
+41
-0
WebMvcTestAllControllersIntegrationTests.java
...web/servlet/WebMvcTestAllControllersIntegrationTests.java
+22
-0
No files found.
spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/validation/ValidationAutoConfiguration.java
View file @
d8d5950e
...
...
@@ -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
()
{
...
...
spring-boot-test-autoconfigure/pom.xml
View file @
d8d5950e
...
...
@@ -173,5 +173,15 @@
<artifactId>
spring-plugin-core
</artifactId>
<scope>
test
</scope>
</dependency>
<dependency>
<groupId>
org.hibernate
</groupId>
<artifactId>
hibernate-validator
</artifactId>
<scope>
test
</scope>
</dependency>
<dependency>
<groupId>
org.apache.tomcat.embed
</groupId>
<artifactId>
tomcat-embed-el
</artifactId>
<scope>
test
</scope>
</dependency>
</dependencies>
</project>
spring-boot-test-autoconfigure/src/main/resources/META-INF/spring.factories
View file @
d8d5950e
...
...
@@ -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
...
...
spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/web/servlet/ExampleController3.java
0 → 100644
View file @
d8d5950e
/*
* 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
;
}
}
spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/web/servlet/WebMvcTestAllControllersIntegrationTests.java
View file @
d8d5950e
...
...
@@ -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"
));
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment