BAEL-175 - Integrtion testing minor changes

This commit is contained in:
slavisa-baeldung 2016-08-02 06:33:49 +02:00
parent 6fb230546d
commit 4f9d9502e2
4 changed files with 136 additions and 133 deletions

View File

@ -15,11 +15,11 @@ import org.springframework.web.servlet.view.JstlView;
@ComponentScan(basePackages = {"com.baeldung.spring.controller"})
public class ApplicationConfig extends WebMvcConfigurerAdapter {
public ApplicationConfig() {
super();
}
@Override
public ApplicationConfig() {
super();
}
@Override
public void addViewControllers(final ViewControllerRegistry registry) {
super.addViewControllers(registry);
registry.addViewController("/").setViewName("index");
@ -27,7 +27,7 @@ public class ApplicationConfig extends WebMvcConfigurerAdapter {
@Bean
public ViewResolver viewResolver() {
final InternalResourceViewResolver bean = new InternalResourceViewResolver();
final InternalResourceViewResolver bean = new InternalResourceViewResolver();
bean.setViewClass(JstlView.class);
bean.setPrefix("/WEB-INF/jsp/");
bean.setSuffix(".jsp");

View File

@ -1,19 +1,22 @@
package com.baeldung.spring.bean;
public class Greeting {
private int id;
private String message;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
private int id;
private String message;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}

View File

@ -11,54 +11,54 @@ import com.baeldung.spring.bean.Greeting;
@Controller
public class GreetController {
@RequestMapping(value = "/homePage", method = RequestMethod.GET)
public String index() {
return "index";
}
@RequestMapping(value = "/greet", method = RequestMethod.GET, produces = "application/json")
@ResponseBody
public Greeting greet() {
Greeting greeting = new Greeting();
greeting.setId(1);
greeting.setMessage("Hello World!!!");
return greeting;
}
@RequestMapping(value = "/greetWithPathVariable/{name}", method = RequestMethod.GET, produces = "application/json")
@ResponseBody
public Greeting greetWithPathVariable(@PathVariable("name") String name) {
Greeting greeting = new Greeting();
greeting.setId(1);
greeting.setMessage("Hello World " + name + "!!!");
return greeting;
}
@RequestMapping(value = "/greetWithQueryVariable", method = RequestMethod.GET, produces = "application/json")
@ResponseBody
public Greeting greetWithQueryVariable(@RequestParam("name") String name) {
Greeting greeting = new Greeting();
greeting.setId(1);
greeting.setMessage("Hello World " + name + "!!!");
return greeting;
}
@RequestMapping(value = "/greetWithPost", method = RequestMethod.POST, produces = "application/json")
@ResponseBody
public Greeting greetWithPost() {
Greeting greeting = new Greeting();
greeting.setId(1);
greeting.setMessage("Hello World!!!");
return greeting;
}
@RequestMapping(value = "/greetWithPostAndFormData", method = RequestMethod.POST, produces = "application/json")
@ResponseBody
public Greeting greetWithPostAndFormData(@RequestParam("id") int id, @RequestParam("name") String name) {
Greeting greeting = new Greeting();
greeting.setId(id);
greeting.setMessage("Hello World " + name + "!!!");
return greeting;
}
@RequestMapping(value = "/homePage", method = RequestMethod.GET)
public String index() {
return "index";
}
@RequestMapping(value = "/greet", method = RequestMethod.GET, produces = "application/json")
@ResponseBody
public Greeting greet() {
Greeting greeting = new Greeting();
greeting.setId(1);
greeting.setMessage("Hello World!!!");
return greeting;
}
@RequestMapping(value = "/greetWithPathVariable/{name}", method = RequestMethod.GET, produces = "application/json")
@ResponseBody
public Greeting greetWithPathVariable(@PathVariable("name") String name) {
Greeting greeting = new Greeting();
greeting.setId(1);
greeting.setMessage("Hello World " + name + "!!!");
return greeting;
}
@RequestMapping(value = "/greetWithQueryVariable", method = RequestMethod.GET, produces = "application/json")
@ResponseBody
public Greeting greetWithQueryVariable(@RequestParam("name") String name) {
Greeting greeting = new Greeting();
greeting.setId(1);
greeting.setMessage("Hello World " + name + "!!!");
return greeting;
}
@RequestMapping(value = "/greetWithPost", method = RequestMethod.POST, produces = "application/json")
@ResponseBody
public Greeting greetWithPost() {
Greeting greeting = new Greeting();
greeting.setId(1);
greeting.setMessage("Hello World!!!");
return greeting;
}
@RequestMapping(value = "/greetWithPostAndFormData", method = RequestMethod.POST, produces = "application/json")
@ResponseBody
public Greeting greetWithPostAndFormData(@RequestParam("id") int id, @RequestParam("name") String name) {
Greeting greeting = new Greeting();
greeting.setId(id);
greeting.setMessage("Hello World " + name + "!!!");
return greeting;
}
}

View File

@ -15,69 +15,69 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view;
public class GreetControllerTest {
private MockMvc mockMvc;
@Before
public void setup() {
this.mockMvc = MockMvcBuilders.standaloneSetup(new GreetController()).build();
}
@Test
public void verifyIndexJspViewName() throws Exception {
this.mockMvc.perform(get("/homePage"))
.andExpect(view().name("index"));
}
@Test
public void verifyGreet() throws Exception {
this.mockMvc.perform(get("/greet"))
.andExpect(status().isOk())
.andExpect(content().contentType("application/json;charset=UTF-8"))
.andExpect(jsonPath("$.message").value("Hello World!!!"));
}
@Test
public void verifyGreetWithPathVariable() throws Exception {
this.mockMvc.perform(get("/greetWithPathVariable/John"))
.andExpect(status().isOk())
.andExpect(content().contentType("application/json;charset=UTF-8"))
.andExpect(jsonPath("$.message").value("Hello World John!!!"));
}
@Test
public void verifyGreetWithPathVariable_2() throws Exception {
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!!!"));
}
@Test
public void verifyGreetWithQueryVariable() throws Exception {
this.mockMvc
.perform(MockMvcRequestBuilders.get("/greetWithQueryVariable").param("name", "John Doe"))
.andDo(print())
.andExpect(MockMvcResultMatchers.status().isOk()).andExpect(MockMvcResultMatchers.content().contentType("application/json;charset=UTF-8"))
.andExpect(MockMvcResultMatchers.jsonPath("$.message").value("Hello World John Doe!!!"));
}
@Test
public void verifyGreetWithPost() throws Exception {
this.mockMvc
.perform(MockMvcRequestBuilders.post("/greetWithPost"))
.andDo(print())
.andExpect(MockMvcResultMatchers.status().isOk())
.andExpect(MockMvcResultMatchers.content().contentType("application/json;charset=UTF-8"))
.andExpect(MockMvcResultMatchers.jsonPath("$.message").value("Hello World!!!"));
}
private MockMvc mockMvc;
@Test
public void verifyGreetWithPostAndFormData() throws Exception {
this.mockMvc
.perform(MockMvcRequestBuilders.post("/greetWithPostAndFormData").param("id", "1").param("name", "John Doe"))
.andDo(print()).andExpect(MockMvcResultMatchers.status().isOk())
.andExpect(MockMvcResultMatchers.content().contentType("application/json;charset=UTF-8"))
.andExpect(MockMvcResultMatchers.jsonPath("$.message").value("Hello World John Doe!!!")).andExpect(MockMvcResultMatchers.jsonPath("$.id").value(1));
}
@Before
public void setup() {
this.mockMvc = MockMvcBuilders.standaloneSetup(new GreetController()).build();
}
@Test
public void verifyIndexJspViewName() throws Exception {
this.mockMvc.perform(get("/homePage"))
.andExpect(view().name("index"));
}
@Test
public void verifyGreet() throws Exception {
this.mockMvc.perform(get("/greet"))
.andExpect(status().isOk())
.andExpect(content().contentType("application/json;charset=UTF-8"))
.andExpect(jsonPath("$.message").value("Hello World!!!"));
}
@Test
public void verifyGreetWithPathVariable() throws Exception {
this.mockMvc.perform(get("/greetWithPathVariable/John"))
.andExpect(status().isOk())
.andExpect(content().contentType("application/json;charset=UTF-8"))
.andExpect(jsonPath("$.message").value("Hello World John!!!"));
}
@Test
public void verifyGreetWithPathVariable_2() throws Exception {
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!!!"));
}
@Test
public void verifyGreetWithQueryVariable() throws Exception {
this.mockMvc
.perform(MockMvcRequestBuilders.get("/greetWithQueryVariable").param("name", "John Doe"))
.andDo(print())
.andExpect(MockMvcResultMatchers.status().isOk()).andExpect(MockMvcResultMatchers.content().contentType("application/json;charset=UTF-8"))
.andExpect(MockMvcResultMatchers.jsonPath("$.message").value("Hello World John Doe!!!"));
}
@Test
public void verifyGreetWithPost() throws Exception {
this.mockMvc
.perform(MockMvcRequestBuilders.post("/greetWithPost"))
.andDo(print())
.andExpect(MockMvcResultMatchers.status().isOk())
.andExpect(MockMvcResultMatchers.content().contentType("application/json;charset=UTF-8"))
.andExpect(MockMvcResultMatchers.jsonPath("$.message").value("Hello World!!!"));
}
@Test
public void verifyGreetWithPostAndFormData() throws Exception {
this.mockMvc
.perform(MockMvcRequestBuilders.post("/greetWithPostAndFormData").param("id", "1").param("name", "John Doe"))
.andDo(print()).andExpect(MockMvcResultMatchers.status().isOk())
.andExpect(MockMvcResultMatchers.content().contentType("application/json;charset=UTF-8"))
.andExpect(MockMvcResultMatchers.jsonPath("$.message").value("Hello World John Doe!!!")).andExpect(MockMvcResultMatchers.jsonPath("$.id").value(1));
}
}