@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