SPRINGMVC Mock Test detailed

Source: Internet
Author: User

@RunWith (Springrunner.class) @SpringBootTest (Classes= Webmanagerapplication.class)//The rollback of the configuration transaction will rollback the database additions and deletions, which facilitates the reuse of the test case.@TransactionConfiguration (TransactionManager = "TransactionManager", Defaultrollback =true) @WebAppConfiguration Public classCollectcontrollertest {PrivateMockmvc Mockmvc;    @Autowired Webapplicationcontext Webapplicationcontext; @Before Public voidSetUp ()throwsException {//mockmvcbuilders using the build Mockmvc object to get the controller through WebapplicationcontextMOCKMVC =Mockmvcbuilders.webappcontextsetup (Webapplicationcontext). build (); //get the controller through the class name, no Webapplicationcontext, no load project configuration//Mockmvc = Mockmvcbuilders.standalonesteup (Usercontroller). build ();    }    /*** Collection Collection*/@Test Public voidCollecttest ()throwsException {//Execute a requestMockmvc.perform (mockmvcrequestbuilders//constructs a request. Post ("/collect/collect")//Adding Parameters//The mock will url the parameters and pass//The parameters added with Param are added to the parameter in Request .//instead of parsing content, the type is added directly to the contents of the request. . param ("Favoritesid", "12"). param ("title", "Test Case"). param ("url", "http://127.0.0.1:8080/")//. Content (COLLECTSTR)//specify what type to pass over. Accept (Mediatype.application_json))//Adding validation assertions. Andexpect (Mockmvcresultmatchers.status (). IsOk ())//Add result processor. Anddo (Mockmvcresulthandlers.print ())//make a custom assertion/make the next asynchronous request. Andreturn (); }}

SPRINGMVC Mock Test detailed

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.