Skip to content

Commit 709da6b

Browse files
Sunil GulabaniSunil Gulabani
authored andcommitted
Updated the test names and added Annotation Based Web Config.
1 parent c996f8c commit 709da6b

5 files changed

Lines changed: 48 additions & 15 deletions

File tree

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.baeldung.web;
2+
3+
import java.util.Set;
4+
5+
import javax.servlet.ServletContext;
6+
import javax.servlet.ServletException;
7+
import javax.servlet.ServletRegistration;
8+
9+
import org.springframework.web.WebApplicationInitializer;
10+
import org.springframework.web.context.ContextLoaderListener;
11+
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
12+
import org.springframework.web.context.support.GenericWebApplicationContext;
13+
import org.springframework.web.servlet.DispatcherServlet;
14+
15+
public class WebAppInitializer implements WebApplicationInitializer {
16+
17+
@Override
18+
public void onStartup(final ServletContext sc) throws ServletException {
19+
20+
final AnnotationConfigWebApplicationContext root = new AnnotationConfigWebApplicationContext();
21+
root.scan("com.baeldung.spring");
22+
23+
sc.addListener(new ContextLoaderListener(root));
24+
25+
final ServletRegistration.Dynamic appServlet = sc.addServlet("spring", new DispatcherServlet(new GenericWebApplicationContext()));
26+
appServlet.setLoadOnStartup(1);
27+
28+
final Set<String> mappingConflicts = appServlet.addMapping("/");
29+
if (!mappingConflicts.isEmpty()) {
30+
throw new IllegalStateException("'appServlet' could not be mapped to '/' due " + "to an existing mapping. This is a known issue under Tomcat versions " + "<= 7.0.14; see https://issues.apache.org/bugzilla/show_bug.cgi?id=51278");
31+
}
32+
}
33+
}

spring-mvc-test/src/main/webapp/WEB-INF/spring-servlet.xml renamed to spring-mvc-test/src/main/webapp/WEB-INF/spring-servlet-old.xml

File renamed without changes.
File renamed without changes.

‎spring-mvc-test/src/test/java/com/baeldung/spring/controller/GreetControllerIntegrationTest.java‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,50 +39,50 @@ public void setup() throws Exception {
3939
}
4040

4141
@Test
42-
public void verifyWac() {
42+
public void givenWAC_whenServletContext_thenItProvidesGreetController() {
4343
ServletContext servletContext = wac.getServletContext();
4444
Assert.assertNotNull(servletContext);
4545
Assert.assertTrue(servletContext instanceof MockServletContext);
4646
Assert.assertNotNull(wac.getBean("greetController"));
4747
}
4848

4949
@Test
50-
public void verifyIndexJspViewName() throws Exception {
50+
public void givenHomePageURI_whenMockMVC_thenReturnsIndexJSPViewName() throws Exception {
5151
this.mockMvc.perform(MockMvcRequestBuilders.get("/homePage")).andDo(print()).andExpect(MockMvcResultMatchers.view().name("index"));
5252
}
5353

5454
@Test
55-
public void verifyGreet() throws Exception {
55+
public void givenGreetURI_whenMockMVC_thenVerifyResponse() throws Exception {
5656
MvcResult mvcResult = this.mockMvc.perform(MockMvcRequestBuilders.get("/greet")).andDo(print()).andExpect(MockMvcResultMatchers.status().isOk()).andExpect(MockMvcResultMatchers.jsonPath("$.message").value("Hello World!!!")).andReturn();
5757
Assert.assertEquals("application/json;charset=UTF-8", mvcResult.getResponse().getContentType());
5858
}
5959

6060
@Test
61-
public void verifyGreetWithPathVariable() throws Exception {
61+
public void givenGreetURIWithPathVariable_whenMockMVC_thenVerifyResponse() throws Exception {
6262
this.mockMvc.perform(MockMvcRequestBuilders.get("/greetWithPathVariable/John")).andDo(print()).andExpect(MockMvcResultMatchers.status().isOk()).andExpect(MockMvcResultMatchers.content().contentType("application/json;charset=UTF-8"))
6363
.andExpect(MockMvcResultMatchers.jsonPath("$.message").value("Hello World John!!!"));
6464
}
6565

6666
@Test
67-
public void verifyGreetWithPathVariable_2() throws Exception {
67+
public void givenGreetURIWithPathVariable_2_whenMockMVC_thenVerifyResponse() throws Exception {
6868
this.mockMvc.perform(MockMvcRequestBuilders.get("/greetWithPathVariable/{name}", "Doe")).andDo(print()).andExpect(MockMvcResultMatchers.status().isOk()).andExpect(MockMvcResultMatchers.content().contentType("application/json;charset=UTF-8"))
6969
.andExpect(MockMvcResultMatchers.jsonPath("$.message").value("Hello World Doe!!!"));
7070
}
7171

7272
@Test
73-
public void verifyGreetWithQueryVariable() throws Exception {
73+
public void givenGreetURIWithQueryParameter_whenMockMVC_thenVerifyResponse() throws Exception {
7474
this.mockMvc.perform(MockMvcRequestBuilders.get("/greetWithQueryVariable").param("name", "John Doe")).andDo(print()).andExpect(MockMvcResultMatchers.status().isOk())
7575
.andExpect(MockMvcResultMatchers.content().contentType("application/json;charset=UTF-8")).andExpect(MockMvcResultMatchers.jsonPath("$.message").value("Hello World John Doe!!!"));
7676
}
7777

7878
@Test
79-
public void verifyGreetWithPost() throws Exception {
79+
public void givenGreetURIWithPost_whenMockMVC_thenVerifyResponse() throws Exception {
8080
this.mockMvc.perform(MockMvcRequestBuilders.post("/greetWithPost")).andDo(print()).andExpect(MockMvcResultMatchers.status().isOk()).andExpect(MockMvcResultMatchers.content().contentType("application/json;charset=UTF-8"))
8181
.andExpect(MockMvcResultMatchers.jsonPath("$.message").value("Hello World!!!"));
8282
}
8383

8484
@Test
85-
public void verifyGreetWithPostAndFormData() throws Exception {
85+
public void givenGreetURIWithPostAndFormData_whenMockMVC_thenVerifyResponse() throws Exception {
8686
this.mockMvc.perform(MockMvcRequestBuilders.post("/greetWithPostAndFormData").param("id", "1").param("name", "John Doe")).andDo(print()).andExpect(MockMvcResultMatchers.status().isOk())
8787
.andExpect(MockMvcResultMatchers.content().contentType("application/json;charset=UTF-8")).andExpect(MockMvcResultMatchers.jsonPath("$.message").value("Hello World John Doe!!!")).andExpect(MockMvcResultMatchers.jsonPath("$.id").value(1));
8888
}

‎spring-mvc-test/src/test/java/com/baeldung/spring/controller/GreetControllerTest.java‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,39 +24,39 @@ public void setup() {
2424
}
2525

2626
@Test
27-
public void verifyIndexJspViewName() throws Exception {
27+
public void givenHomePageURI_whenMockMVC_thenReturnsIndexJSPViewName() throws Exception {
2828
this.mockMvc.perform(get("/homePage")).andExpect(view().name("index"));
2929
}
3030

3131
@Test
32-
public void verifyGreet() throws Exception {
32+
public void givenGreetURI_whenMockMVC_thenVerifyResponse() throws Exception {
3333
this.mockMvc.perform(get("/greet")).andExpect(status().isOk()).andExpect(content().contentType("application/json;charset=UTF-8")).andExpect(jsonPath("$.message").value("Hello World!!!"));
3434
}
3535

3636
@Test
37-
public void verifyGreetWithPathVariable() throws Exception {
37+
public void givenGreetURIWithPathVariable_whenMockMVC_thenVerifyResponse() throws Exception {
3838
this.mockMvc.perform(get("/greetWithPathVariable/John")).andExpect(status().isOk()).andExpect(content().contentType("application/json;charset=UTF-8")).andExpect(jsonPath("$.message").value("Hello World John!!!"));
3939
}
4040

4141
@Test
42-
public void verifyGreetWithPathVariable_2() throws Exception {
42+
public void givenGreetURIWithPathVariable_2_whenMockMVC_thenVerifyResponse() throws Exception {
4343
this.mockMvc.perform(get("/greetWithPathVariable/{name}", "Doe")).andExpect(status().isOk()).andExpect(content().contentType("application/json;charset=UTF-8")).andExpect(jsonPath("$.message").value("Hello World Doe!!!"));
4444
}
4545

4646
@Test
47-
public void verifyGreetWithQueryVariable() throws Exception {
47+
public void givenGreetURIWithQueryParameter_whenMockMVC_thenVerifyResponse() throws Exception {
4848
this.mockMvc.perform(MockMvcRequestBuilders.get("/greetWithQueryVariable").param("name", "John Doe")).andDo(print()).andExpect(MockMvcResultMatchers.status().isOk())
4949
.andExpect(MockMvcResultMatchers.content().contentType("application/json;charset=UTF-8")).andExpect(MockMvcResultMatchers.jsonPath("$.message").value("Hello World John Doe!!!"));
5050
}
5151

5252
@Test
53-
public void verifyGreetWithPost() throws Exception {
53+
public void givenGreetURIWithPost_whenMockMVC_thenVerifyResponse() throws Exception {
5454
this.mockMvc.perform(MockMvcRequestBuilders.post("/greetWithPost")).andDo(print()).andExpect(MockMvcResultMatchers.status().isOk()).andExpect(MockMvcResultMatchers.content().contentType("application/json;charset=UTF-8"))
5555
.andExpect(MockMvcResultMatchers.jsonPath("$.message").value("Hello World!!!"));
5656
}
5757

5858
@Test
59-
public void verifyGreetWithPostAndFormData() throws Exception {
59+
public void givenGreetURIWithPostAndFormData_whenMockMVC_thenVerifyResponse() throws Exception {
6060
this.mockMvc.perform(MockMvcRequestBuilders.post("/greetWithPostAndFormData").param("id", "1").param("name", "John Doe")).andDo(print()).andExpect(MockMvcResultMatchers.status().isOk())
6161
.andExpect(MockMvcResultMatchers.content().contentType("application/json;charset=UTF-8")).andExpect(MockMvcResultMatchers.jsonPath("$.message").value("Hello World John Doe!!!")).andExpect(MockMvcResultMatchers.jsonPath("$.id").value(1));
6262
}

0 commit comments

Comments
 (0)