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.
Spring Cloud Spring Boot mybatis Distributed microservices Cloud Architecture (vi) RESTFU