Spring Cloud Spring Boot mybatis Distributed microservices Cloud Architecture (vi) RESTFU

Source: Internet
Author: User

The following test case validation correctness is written for the controller, as follows. Of course, the browser plug-in can also be used to submit validation requests.

@RunWith (Springjunit4cla***unner.class) @SpringApplicationConfiguration (classes = mockservletcontext.class) @     Webappconfiguration public class Applicationtests {private MOCKMVC mvc; @Before public void SetUp () throws Exception {MVC = Mockmvcbuilders.standalonesetup (New Usercontroller ()). Bui     LD (); } @Test public void Testusercontroller () throws Exception {//test Usercontroller Requestbuilder req         Uest = null;         1, get check the user list, should be empty request = Get ("/users/"); Mvc.perform (Request) Andexpect (status () IsOk ()). Andexpect (Content (). String (Equalto ("[]")         )); 2. Post submits a user request = post ("/users/"). PARAM ("id", "1"). Param ("name", "Test Master"         "). Param (" Age "," 20 ");         Mvc.perform (Request) Andexpect (content (). String (Equalto ("Success"));         3, get get the user list, should have just inserted the data request = Get ("/users/"); MVc.perform (Request). Andexpect (Status (). IsOk ()). Andexpect (Content (). String (Equalto ("[{\" I         D\ ": 1,\" name\ ": \" Test master \ ", \" age\ ": 20}]")); 4, put modifies the user request with id 1 = put ("/users/1"). Param ("name", "Test Ultimate Master"). Param ("Age         "," 30 ");         Mvc.perform (Request) Andexpect (content (). String (Equalto ("Success"));         5. Get a user request with id 1 = Get ("/USERS/1");         Mvc.perform (Request). Andexpect (Content (). String (Equalto ("{\" id\ ": 1,\" name\ ": \" test Ultimate master \ ", \" age\ ": 30}")));         6. del Delete user request with id 1 = delete ("/USERS/1");         Mvc.perform (Request) Andexpect (content (). String (Equalto ("Success"));         7, get check the user list, should be empty request = Get ("/users/"); Mvc.perform (Request) Andexpect (status () IsOk ()). Andexpect (Content (). String (Equalto ("[]")     )); } }

At this point, by introducing a Web module (without any other configuration), we can easily take advantage of the functionality of Spring MVC and complete the creation of a restful API for user objects and the writing of unit tests with very simple code. It also introduces some of the most commonly used core annotations in spring MVC: @Controller, @RestController, requestmapping, and some parameter binding annotations: @PathVariable, @ Modelattribute, @RequestParam and so on. Source source technical support for complete projects 1791743380

Spring Cloud Spring Boot mybatis Distributed microservices Cloud Architecture (vi) RESTFU

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.