Method Validation doesn't work in Tests using MockMvc on SpringBoot version 1.5.0.BUILD-SNAPSHOT
See original GitHub issueI wanted to use bean-validation for parameters of a @RestController method, as mentioned in #6228
I am using SpringBoot version 1.5.0.BUILD-SNAPSHOT and wanted to use method validation by adding a @Validated annotation on my controller and then constraints on my method.
@RestController
@Validated
public class HelloController {
@RequestMapping("/")
public String index() {
return "Greetings from Spring Boot!";
}
@RequestMapping(value = "/{uid}", method = RequestMethod.GET)
public String userById(@PathVariable @Size(max = 12) final String uid) {
return "Hello :"+uid;
}
@ExceptionHandler(value = {ConstraintViolationException.class})
@ResponseStatus(value = HttpStatus.BAD_REQUEST)
public String handleValidationFailure(ConstraintViolationException ex) {
StringBuilder messages = new StringBuilder();
for (ConstraintViolation<?> violation : ex.getConstraintViolations()) {
messages.append(violation.getMessage() + "\n");
}
return messages.toString();
}
The validation works when I start up my service but my test in MockMvc fails against expected behavior of throwing a Constraint Violation exception when a constraint is violated.
The below test should fail due to violation of size constraint since I’ve set @Size(max = 12)
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = Application.class)
@WebAppConfiguration
@ActiveProfiles(profiles = "test")
public class HelloControllerTest {
@Autowired
private WebApplicationContext webApplicationContext;
private MockMvc mockMvc;
@Before
public void setUp() throws Exception {
mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
}
@Test
public void getUserException() throws Exception {
mockMvc.perform(get("/123222333322223"))
.andExpect(status().isOk())
.andExpect(content().string(equalToIgnoringCase("Hello :123222333322223")));
}
}
My pom.xml file looks like
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.springframework</groupId>
<artifactId>gs-spring-boot</artifactId>
<version>0.1.0</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.0.BUILD-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
<version>1.5.0.BUILD-SNAPSHOT</version>
</dependency>
</dependencies>
<properties>
<java.version>1.8</java.version>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/libs-snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
</project>
I am working off the sample Spring boot project from https://github.com/spring-guides/gs-spring-boot.git
Issue Analytics
- State:
- Created 7 years ago
- Comments:8 (4 by maintainers)
Top Results From Across the Web
Spring Boot Reference Documentation
Try the How-to documents. They provide solutions to the most common questions. Learn the Spring basics. Spring Boot builds on many other Spring...
Read more >Spring Boot Reference Guide
This section provides a brief overview of Spring Boot reference documentation. Think of it as map for the rest of the document. You...
Read more >Spring Boot Reference Guide
This section goes into more detail about how you should use Spring Boot. It covers topics such as build systems, auto-configuration and run/deployment...
Read more >Spring Boot Reference Guide
This section provides a brief overview of Spring Boot reference documentation. Think of it as map for the rest of the document. You...
Read more >Spring Boot Reference Guide
Upgrading from an earlier version of Spring Boot . ... Changing the Java version . ... You don't need to use the CLI...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Validation is not enabled by default by
@WebMvcTest(slice tests switch off auto-config and only enables a few of them explicitly). I guess we could add it to the list for@WebMvcTest.If you add following snippet it should work as it worked for me as well.