Spring-test
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {MyMvcConfig.class})
@WebAppConfiguration("src/main/resources")
public class TestControllerIntegrationTests {
private MockMvc mockMvc;
@Autowired
private DemoService demoService;
@Autowired
WebApplicationContext wac;
@Autowired
MockHttpSession session;
@Autowired
MockHttpServletRequest request;
@Before
public void setup() {
mockMvc =
MockMvcBuilders.webAppContextSetup(this.wac).build();
}
@Test
public void testNormalController() throws Exception{
mockMvc.perform(get("/normal"))
.andExpect(status().isOk())
.andExpect(view().name("page"))
.andExpect(forwardedUrl("/WEB-INF/classes/views/page.jsp"))
.andExpect(model().attribute("msg", demoService.saySomething()));
}
@Test
public void testRestController() throws Exception{
mockMvc.perform(get("/testRest"))
.andExpect(status().isOk())
.andExpect(content().contentType("text/plain;charset=UTF-8"))
.andExpect(content().string(demoService.saySomething()));
}
}