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