1) The simulated MVC test is the same as the basic test and requires the introduction of JUnit support in the Pom file.
Slightly
2) Write test class Application1testmvc
Add new annotations to @runwith (Springrunner.class), @RunWith (Springrunner.class) In addition to the class header
@AutoConfigureMockMvc//Injection MOCKMVC
(Of course you really do not want to add, there are other ways, but I do not want to say, trouble)
1 PackageCom.cx.springboot;2 3 Importjava.util.Date;4 5 Importorg.junit.Test;6 ImportOrg.junit.runner.RunWith;7 Importorg.springframework.beans.factory.annotation.Autowired;8 ImportORG.SPRINGFRAMEWORK.BOOT.TEST.AUTOCONFIGURE.WEB.SERVLET.AUTOCONFIGUREMOCKMVC;9 Importorg.springframework.boot.test.context.SpringBootTest;Ten ImportOrg.springframework.http.MediaType; One ImportOrg.springframework.mock.web.MockHttpServletResponse; A ImportOrg.springframework.test.context.junit4.SpringRunner; - ImportORG.SPRINGFRAMEWORK.TEST.WEB.SERVLET.MOCKMVC; - ImportOrg.springframework.test.web.servlet.MvcResult; the Importorg.springframework.test.web.servlet.request.MockMvcRequestBuilders; - - ImportCom.alibaba.fastjson.JSON; - ImportCom.cx.springboot.hello1.model.UserModel; + -@RunWith (Springrunner.class) + @SpringBootTest A@AutoConfigureMockMvc//Inject Mockmvc at Public classAPPLICATION1TESTMVC { - - @Autowired - PrivateMOCKMVC MVC; - - /** in * - * @throwsException to * @ creation Time July 13, 2018 + * @ Function Description Pass value by link, accept string return value - */ the @Test * Public voidTestString ()throwsException { $ //prepare the request URL without the map address of the direct write interface with IP, port, project name, etc.Panax NotoginsengString url = "/APP/GET2/ZHANGSAN/1"; - the /*Build request send request GET requests + * There are many request methods in Mockmvcrequestbuilders. Like get, post, put, delete, etc. A */ theMvcresult Mvcresult =mvc.perform (mockmvcrequestbuilders.get (URL) +. Accept (Mediatype.application_json))//asserts that the returned result is JSON -. Andreturn ();//Get returned results $ $Mockhttpservletresponse response =mvcresult.getresponse (); - //get the request return code - intStatus =response.getstatus (); the //get the results . -String contentasstring =response.getcontentasstring ();Wuyi the System.err.println (status); - System.err.println (contentasstring); Wu } - About $ - /** - * - * @throwsException A * @ creation Time July 13, 2018 + * @ Function Description Pass header, accept return value the */ - @Test $ Public voidHeadertest ()throwsException { the //URI theString uri = "/app/get4"; the theMvcresult Mvcresult =mvc.perform (Mockmvcrequestbuilders.get (URI) -. Header ("token", "asd123") in. Header ("name", "Zhangsan11") the. Accept (Mediatype.application_json))//asserts that the returned result is JSON the. Andreturn ();//Get returned results About theMockhttpservletresponse response =mvcresult.getresponse (); the //get the request return code the intStatus =response.getstatus (); + //get the results . -String contentasstring =response.getcontentasstring (); the Bayi System.err.println (status); the System.err.println (contentasstring); the } - /** - * the * @throwsException the * @ creation Time July 13, 2018 the * @ Function Description Pass POST request and Bean type Object, accept return value the */ - @Test the Public voidPosttest ()throwsException { the //URI theString uri = "/app/get3";94 theUsermodel Usermodel =NewUsermodel ("Zhang San", 11,NewDate (), "abc123"); the theMvcresult Mvcresult =mvc.perform (Mockmvcrequestbuilders.post (URI)98 . ContentType (Mediatype.application_json_utf8) About . Content (Json.tojsonstring (Usermodel)) -. Accept (Mediatype.application_json))//asserts that the returned result is JSON101. Andreturn ();//Get returned results102 103Mockhttpservletresponse response =mvcresult.getresponse ();104 //get the request return code the intStatus =response.getstatus ();106 //get the results .107String contentasstring =response.getcontentasstring ();108 109 System.err.println (status); the System.err.println (contentasstring);111 } the}
Springboot Unit Test-simulated MVC test