RESTful API Unit Testing

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 ())       . build (); } @Test public void Testusercontroller () throws Exception {//test Usercontroller Requestbui           Lder request = 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 user list, should have justOnly the data that is inserted is request = Get ("/users/"); Mvc.perform (Request) Andexpect (status () IsOk ()). Andexpect (Content (). String (Equalto ("           [{\ "id\": 1,\ "name\": \ "Test master \", \ "age\": 20}])); 4, put modifies the user request with id 1 = put ("/users/1"). Param ("name", "Test Ultimate Master"). Para           M ("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.

RESTful API Unit Testing

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.