please help me to solve the following issue: I have a class, where several fields are marked as @NotNull:
public class SearchCommentRequest {
@NotNull
private Date fromDate;
@NotNull
private Date toDate;
//...
}
Object if this class is passed to controller as @RequestBody annotated also with @Valid:
@PostMapping(value = "/comment/search", consumes="application/json", produces = "text/csv")
public ResponseEntity<byte[]> searchComments(@RequestBody @Valid SearchCommentRequest searchRequest) {
List<SearchCommentResult> comments = commentService.searchComments(searchRequest);
So, I expect that if either fromDate or toDate is null - exception will be thrown.
Writing my integration tests, I decided to check this validation case as well:
@Test
public void searchCommentsValidateRequest() throws Exception {
ObjectMapper mapper = new ObjectMapper();
SearchCommentRequest request = new SearchCommentRequest();
// toDate = null;
request.setFromDate(new Date());
String requestBody = mapper.writer().writeValueAsString(request);
mockMvc.perform(post(COMMENT_SEARCH_ENDPOINT)
.contentType("application/json")
.content(requestBody))
.andDo(MockMvcResultHandlers.print())
.andExpect(status().is(400));
}
But it looks like mockMvc is ignoring validation. Searching for the same issues, I found several sources where solution was adding the following dependencies:
<dependency>
<groupId>javax.el</groupId>
<artifactId>javax.el-api</artifactId>
<version>2.2.4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.glassfish</groupId>
<artifactId>javax.el</artifactId>
<version>3.0.0</version>
</dependency>
But it didn't help. I'm using Spring 4.3.3.RELEASE and manually added to pom.xml the following dependency:
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>2.0.1.Final</version>
</dependency>