Spring 4.0 junit Simple Controller test

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.