Skip to content

Instantly share code, notes, and snippets.

@rajivrnair
Created November 14, 2017 04:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rajivrnair/627961c5cde37fcf768cab68c9f46a43 to your computer and use it in GitHub Desktop.
Save rajivrnair/627961c5cde37fcf768cab68c9f46a43 to your computer and use it in GitHub Desktop.
Testing the Form Post in Spring Boot/MVC
public class Ping {
private String message = "";
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
@Controller
public class PingController {
@PostMapping("/ping")
public String transactionComplete(@ModelAttribute Ping response) {
return "pong"; //template name
}
}
@RunWith(SpringRunner.class)
@WebMvcTest(PingController.class)
public class PingControllerIntegrationTest {
private static final String MESSAGE = "'sup yo?";
@Autowired
private MockMvc mockMvc;
@Test
public void endpoint_exists() throws Exception {
this.mockMvc.perform(post("/ping")).
andExpect(status().isOk());
}
@Test
public void attribute_value_is_mapped_correctly() throws Exception {
this.mockMvc.perform(post("/ping")
.contentType(MediaType.APPLICATION_FORM_URLENCODED)
.content(encode("Message", "UTF-8") + "=" + encode(MESSAGE, "UTF-8")))
.andExpect(status().isOk())
.andExpect(model().attribute("ping", new BaseMatcher<Ping>() {
@Override
public boolean matches(Object response) {
return MESSAGE.equals(((Ping)response).getMessage());
}
@Override
public void describeTo(Description description) {
}
}));
}
@Test
public void page_is_rendered_with_message() throws Exception {
this.mockMvc.perform(post("/ping")
.contentType(MediaType.APPLICATION_FORM_URLENCODED)
.content(encode("Message", "UTF-8") + "=" + encode(MESSAGE, "UTF-8")))
.andExpect(status().isOk())
.andExpect(content().string(new BaseMatcher<String>() {
@Override
public boolean matches(Object item) {
return ((String) item).contains("<h1>Message: &#39;sup yo?</h1>");
}
@Override
public void describeTo(Description description) {
}
}));
}
@Test
public void page_is_rendered_without_message() throws Exception {
this.mockMvc.perform(post("/ping")
.contentType(MediaType.APPLICATION_FORM_URLENCODED))
.andExpect(status().isOk())
.andExpect(content().string(new BaseMatcher<String>() {
@Override
public boolean matches(Object item) {
return ((String) item).contains("<h1>No message rcvd</h1>");
}
@Override
public void describeTo(Description description) {
}
}));
}
}
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>PING</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<h1 th:text="${ping.message == ''} ? 'No message rcvd' : 'Message: ' + ${ping.message}" />
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment