Skip to content
This repository was archived by the owner on Dec 15, 2021. It is now read-only.

Commit c93c892

Browse files
committed
Merge pull request #111 from e-g-hategan/master
Adjusted tests for SPR-13490
2 parents 3daff50 + 0319caf commit c93c892

File tree

2 files changed

+22
-5
lines changed

2 files changed

+22
-5
lines changed

SPR-13490/src/main/java/org/springframework/issues/TestController.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,13 @@ public void initBinder(WebDataBinder binder) {
1515
binder.addValidators(new PayloadValidator());
1616
}
1717

18-
@RequestMapping("/")
19-
public String emptyBody(@RequestBody @Validated Payload payload) {
18+
@RequestMapping("/mandatory-body")
19+
public String mandatoryBody(@RequestBody @Validated Payload payload) {
20+
return "hello";
21+
}
22+
23+
@RequestMapping("/optional-body")
24+
public String optionalBody(@RequestBody(required = false) @Validated Payload payload) {
2025
return "hello";
2126
}
2227
}

SPR-13490/src/test/java/org/springframework/issues/TestControllerTest.java

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,31 @@ public void prepare() {
2424

2525
@Test
2626
public void shouldSend200WithValidPayload() throws Exception {
27-
mockMvc.perform(post("/").content("{\"message\":\"spring\"}").contentType(MediaType.APPLICATION_JSON))
27+
mockMvc.perform(post("/mandatory-body").content("{\"message\":\"spring\"}").contentType(MediaType.APPLICATION_JSON))
2828
.andExpect(status().isOk())
2929
.andExpect(content().string("\"hello\""));
3030
}
3131

3232
@Test
3333
public void shouldSend400WithEmptyBody() throws Exception {
34-
mockMvc.perform(post("/")).andExpect(status().isBadRequest());
34+
mockMvc.perform(post("/mandatory-body")).andExpect(status().isBadRequest());
3535
}
3636

3737
@Test
3838
public void shouldSend400WithEmptyJsonObject() throws Exception {
39-
mockMvc.perform(post("/").content("{}").contentType(MediaType.APPLICATION_JSON))
39+
mockMvc.perform(post("/mandatory-body").content("{}").contentType(MediaType.APPLICATION_JSON))
4040
.andExpect(status().isBadRequest());
4141
}
42+
43+
@Test
44+
public void shouldSend400WithNullJsonObject() throws Exception {
45+
mockMvc.perform(post("/mandatory-body").content("null").contentType(MediaType.APPLICATION_JSON))
46+
.andExpect(status().isBadRequest());
47+
}
48+
49+
@Test
50+
public void shouldSend200WithNullJsonObject() throws Exception {
51+
mockMvc.perform(post("/optional-body").content("null").contentType(MediaType.APPLICATION_JSON))
52+
.andExpect(status().isOk());
53+
}
4254
}

0 commit comments

Comments
 (0)