A little more complicated than DAO and service testing. or write a basicwebtest for the overall configuration:
@WebAppConfiguration @contextconfiguration (Locations= {"Classpath:spring/applicationcontext.xml","Classpath:spring/spring-servlet.xml"}) Public classBasicwebtest extends Abstracttransactionaljunit4springcontexttests {@AutowiredprotectedWebapplicationcontext WAC; protectedMOCKMVC MVC; @Before Public voidsetUp () { This. mvc=Mockmvcbuilders.webappcontextsetup (WAC). build (); }}
basicwebtest @WebAppConfiguration Annotations, and there are a number of methods and fields in the class. Just copy it.
The test cases are as follows:
Package Com.suyin.controller;import Java.util.list;import org.hamcrest.description;import org.hamcrest.Matcher ; Import Org.junit.test;import Org.springframework.mock.web.mockhttpservletrequest;import Org.springframework.mock.web.mockhttpservletresponse;import Org.springframework.test.web.servlet.MvcResult; Import Org.springframework.test.web.servlet.request.mockmvcrequestbuilders;import Org.springframework.test.web.servlet.result.mockmvcresultmatchers;import Com.jayway.jsonpath.jsonpath;import Com.suyin.basicwebtest;import com.suyin.pojo.People; @SuppressWarnings ("unchecked") Public classPeoplecontrollertest extends Basicwebtest {@Test Public voidQueryall () throws Exception {Mvc.perform (mockmvcrequestbuilders.Get("/people/queryall") . Andexpect (Mockmvcresultmatchers.status (). IsOk ()). Andexpect (MOCKMVCRESULTMATCHERS.VI EW (). Name ("people/list"). Andexpect (Mockmvcresultmatchers.request (). Attribute ("List",NewMatcheradapter () {@Override Public voidDescribemismatch (Object item, Description mismatchdescription) {Mismatchdescription.appendtex T ("Wrong list size"); } @Override PublicBoolean matches (Object item) {List<People> list= (list<people>) Item; returnList.size () = =4; } })); } @Test Public voidAsynqueryall () throws Exception {Mvcresult Mr=mvc.perform (mockmvcrequestbuilders.Get("/people/asynqueryall"). Content ("{\ "id\": 1}") . Andexpect (Mockmvcresultmatchers.status (). IsOk ()). Andexpect (Mockmvcresultmatchers.js Onpath ("$.id"). Exists ()). Andreturn (); Mockhttpservletresponse Response=Mr.getresponse (); Mockhttpservletrequest Request=mr.getrequest (); } @Test Public voidupdate1 () throws Exception {Mvc.perform (Mockmvcrequestbuilders.post ("/people/update"). Param ("ID"," in"). param ("name","Jack"). param ("Address","Japan"). param (" Age"," +") . Andexpect (Mockmvcresultmatchers.status (). IsOk ()). Andexpect (MOCKMVCRESULTMATCHERS.VI EW (). Name ("People/detail")); } @Test Public voidUpdate2 () throws Exception {Mvc.perform (Mockmvcrequestbuilders.post ("/people/update") //. Param ("name", "Jack"). param ("Address","Japan"). param (" Age"," +") . Andexpect (Mockmvcresultmatchers.status (). IsOk ()). Andexpect (MOCKMVCRESULTMATCHERS.VI EW (). Name ("Error")); } @Test Public voidAdd1 () throws Exception {Mvc.perform (Mockmvcrequestbuilders.post ("/people/add"). param ("name","JJJJ"). param ("Address",""). param (" Age","1") . Andexpect (Mockmvcresultmatchers.status (). IsOk ()). Andexpect (MOCKMVCR Esultmatchers.view (). Name ("People/detail")); } @Test Public voidAdd2 () throws Exception {Mvc.perform (Mockmvcrequestbuilders.post ("/people/add"). param ("Address",""). param (" Age","1") . Andexpect (Mockmvcresultmatchers.status (). IsOk ()). Andexpect (Mockmvc Resultmatchers.view (). Name ("Error")); } Abstract classMatcheradapter<t> Implements Matcher<t>{@Override Public voidDescribeto (Description arg0) {//TODO auto-generated Method Stub} @Override Public void_dont_implement_matcher___instead_extend_basematcher_ () {//TODO auto-generated Method Stub} @Override Public Abstract voidDescribemismatch (Object item, Description mismatchdescription); @Override Public AbstractBoolean matches (Object item); }}
View Code
Seriously, the spring test on the controller is that. In more is about MOCKMVC's API learning.
Spring 4.0 junit Simple Controller test