

add an address to a library: curl -i -X PUT -d “http://localhost:8080/addresses/1” -H “Content-Type:text/uri-list” http://localhost:8080/libraries/1/libraryAddress
POST requests to the /authors collection resource: curl -i -X POST -H “Content-Type:application/json” -d “{"name":"author1"}” http://localhost:8080/authors


@Autowired private WebApplicationContext webApplicationContext;

@Test
public void givenWac_whenServletContext_thenItProvidesGreetController() {
ServletContext servletContext = webApplicationContext.getServletContext();
Assert.assertNotNull(servletContext);
Assert.assertTrue(servletContext instanceof MockServletContext);
Assert.assertNotNull(webApplicationContext.getBean("greetController"));
}
http://localhost:8080/spring-mvc-test/
@Test
public void givenHomePageURI_whenMockMVC_thenReturnsIndexJSPViewName() {
this.mockMvc.perform(get("/homePage")).andDo(print())
.andExpect(view().name("index"));
}
We’ll invoke the /greet endpoint from our test as:
http://localhost:8080/spring-mvc-test/greet
The expected output will be:
{
"id": 1,
"message": "Hello World!!!"
}
Let’s see the test code:
@Test
public void givenGreetURI_whenMockMVC_thenVerifyResponse() {
MvcResult mvcResult = this.mockMvc.perform(get("/greet"))
.andDo(print()).andExpect(status().isOk())
.andExpect(jsonPath("$.message").value("Hello World!!!"))
.andReturn();
Assert.assertEquals("application/json;charset=UTF-8",
mvcResult.getResponse().getContentType());
}
invoke the /greetWithPathVariable/{name} endpoint from our test as:
http://localhost:8080/spring-mvc-test/greetWithPathVariable/John
The expected output will be:
{
"id": 1,
"message": "Hello World John!!!"
}
Let’s see the test code:
@Test
public void givenGreetURIWithPathVariable_whenMockMVC_thenResponseOK() {
this.mockMvc
.perform(get("/greetWithPathVariable/{name}", "John"))
.andDo(print()).andExpect(status().isOk())
.andExpect(content().contentType("application/json;charset=UTF-8"))
.andExpect(jsonPath("$.message").value("Hello World John!!!"));
}
invoke the /greetWithQueryVariable?name={name} endpoint from our test as:
http://localhost:8080/spring-mvc-test/greetWithQueryVariable?name=John%20Doe
the expected output will be:
{
"id": 1,
"message": "Hello World John Doe!!!"
}
the test code:
@Test
public void givenGreetURIWithQueryParameter_whenMockMVC_thenResponseOK() {
this.mockMvc.perform(get("/greetWithQueryVariable")
.param("name", "John Doe")).andDo(print()).andExpect(status().isOk())
.andExpect(content().contentType("application/json;charset=UTF-8"))
.andExpect(jsonPath("$.message").value("Hello World John Doe!!!"));
}
invoke the /greetWithPost endpoint from our test as:
http://localhost:8080/spring-mvc-test/greetWithPost
output:
{
"id": 1,
"message": "Hello World!!!"
}
And test code is:
@Test
public void givenGreetURIWithPost_whenMockMVC_thenVerifyResponse() {
this.mockMvc.perform(post("/greetWithPost")).andDo(print())
.andExpect(status().isOk()).andExpect(content()
.contentType("application/json;charset=UTF-8"))
.andExpect(jsonPath("$.message").value("Hello World!!!"));
}