Package Com.sishuok.mvc.controller;
Import Org.junit.Assert;
Import Org.junit.Before;
Import Org.junit.Test;
Import Org.junit.runner.RunWith;
Import org.springframework.beans.factory.annotation.Autowired;
Import org.springframework.test.context.ContextConfiguration;
Import Org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
Import org.springframework.test.context.web.WebAppConfiguration;
Import ORG.SPRINGFRAMEWORK.TEST.WEB.SERVLET.MOCKMVC;
Import Org.springframework.test.web.servlet.MvcResult;
Import Org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
Import Org.springframework.test.web.servlet.result.MockMvcResultHandlers;
Import Org.springframework.test.web.servlet.result.MockMvcResultMatchers;
Import Org.springframework.test.web.servlet.setup.MockMvcBuilders;
Import Org.springframework.web.context.WebApplicationContext;
Import Com.sishuok.service.UserService;
@RunWith (Springjunit4classrunner.class)
@WebAppConfiguration (value = "Src/main/webapp")
@ContextConfiguration (value = {"Classpath:spring-config.xml", "Classpath:spring-mvc.xml"})
public class Usercontrollerwebappcontextsetuptest {
@Autowired
UserService UserService;
@Autowired
Private Webapplicationcontext WAC;
Private MOCKMVC Mockmvc;
@Before
public void SetUp () {
MOCKMVC = Mockmvcbuilders.webappcontextsetup (WAC). build ();
}
@Test
public void TestView () throws Exception {
Mvcresult result = Mockmvc.perform (Mockmvcrequestbuilders.get ("/USER/1"))
. Andexpect (Mockmvcresultmatchers.view (). Name ("User/view"))
. Andexpect (Mockmvcresultmatchers.model (). Attributeexists ("user"))
. Anddo (Mockmvcresulthandlers.print ())
. Andreturn ();
Assert.assertnotnull (Result.getmodelandview (). Getmodel (). Get ("user"));
}
}
The most important of these are three objects
Webapplicationcontext
Mockmvc
Mockactionresponse
Mockactionresquest
This is simple, and I'll add it back.
Spring MVC Unit Test