|
| 1 | +package org.baeldung.controller; |
| 2 | + |
| 3 | +import javax.servlet.ServletContext; |
| 4 | + |
| 5 | +import org.baeldung.controller.config.WebConfig; |
| 6 | +import org.baeldung.controller.student.Student; |
| 7 | +import org.junit.Assert; |
| 8 | +import org.junit.Before; |
| 9 | +import org.junit.Test; |
| 10 | +import org.junit.runner.RunWith; |
| 11 | +import org.springframework.beans.factory.annotation.Autowired; |
| 12 | +import org.springframework.test.context.ContextConfiguration; |
| 13 | +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; |
| 14 | +import org.springframework.test.context.support.AnnotationConfigContextLoader; |
| 15 | +import org.springframework.test.context.web.WebAppConfiguration; |
| 16 | +import org.springframework.test.web.servlet.MockMvc; |
| 17 | +import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; |
| 18 | +import org.springframework.test.web.servlet.setup.MockMvcBuilders; |
| 19 | +import org.springframework.web.context.WebApplicationContext; |
| 20 | +import org.springframework.web.servlet.ModelAndView; |
| 21 | + |
| 22 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 23 | + |
| 24 | + |
| 25 | +@RunWith(SpringJUnit4ClassRunner.class) |
| 26 | +@WebAppConfiguration |
| 27 | +@ContextConfiguration(classes={WebConfig.class},loader=AnnotationConfigContextLoader.class ) |
| 28 | +public class ControllerAnnotationTest { |
| 29 | + |
| 30 | + @Autowired |
| 31 | + private MockMvc mockMvc; |
| 32 | + |
| 33 | + @Autowired |
| 34 | + private WebApplicationContext wac; |
| 35 | + |
| 36 | + @Autowired |
| 37 | + private ServletContext servletContext; |
| 38 | + |
| 39 | + private Student selectedStudent; |
| 40 | + |
| 41 | + @Before |
| 42 | + public void setUp() { |
| 43 | + this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build(); |
| 44 | + |
| 45 | + selectedStudent = new Student(); |
| 46 | + selectedStudent.setId(1); |
| 47 | + selectedStudent.setName("Peter"); |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + public void testTestController() throws Exception { |
| 52 | + |
| 53 | + ModelAndView mv = this.mockMvc.perform(MockMvcRequestBuilders.get("/test/")) |
| 54 | + .andReturn() |
| 55 | + .getModelAndView(); |
| 56 | + |
| 57 | + // validate modal data |
| 58 | + Assert.assertSame(mv.getModelMap().get("data").toString(), "Welcome home man"); |
| 59 | + |
| 60 | + // validate view name |
| 61 | + Assert.assertSame(mv.getViewName(), "welcome"); |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + public void testRestController() throws Exception { |
| 66 | + |
| 67 | + String responseBody = this.mockMvc.perform(MockMvcRequestBuilders.get("/student/{studentId}", 1)) |
| 68 | + .andReturn().getResponse() |
| 69 | + .getContentAsString(); |
| 70 | + |
| 71 | + ObjectMapper reader = new ObjectMapper(); |
| 72 | + |
| 73 | + Student studentDetails = reader.readValue(responseBody, Student.class); |
| 74 | + |
| 75 | + Assert.assertEquals(selectedStudent, studentDetails); |
| 76 | + |
| 77 | + } |
| 78 | + |
| 79 | + @Test |
| 80 | + public void testRestAnnotatedController() throws Exception { |
| 81 | + |
| 82 | + String responseBody = this.mockMvc.perform(MockMvcRequestBuilders.get("/annotated/student/{studentId}", 1)) |
| 83 | + .andReturn().getResponse() |
| 84 | + .getContentAsString(); |
| 85 | + |
| 86 | + ObjectMapper reader = new ObjectMapper(); |
| 87 | + |
| 88 | + Student studentDetails = reader.readValue(responseBody, Student.class); |
| 89 | + |
| 90 | + Assert.assertEquals(selectedStudent, studentDetails); |
| 91 | + } |
| 92 | + |
| 93 | +} |
0 commit comments