The following is a test instance of the user controller
Import static org.junit.assert.*;import java.util.arraylist;import java.util.list;import javassist.expr.NewArray;import org.hamcrest.Matchers;import org.junit.Before;import org.junit.test;import org.junit.runner.runwith;import Org.springframework.beans.factory.annotation.autowired;import org.springframework.http.mediatype;import org.springframework.test.context.ContextConfiguration;import org.springframework.test.context.junit4.springjunit4classrunner;import org.springframework.test.context.web.webappconfiguration;import org.springframework.test.web.servlet.mockmvc;import org.springframework.test.web.servlet.result.mockmvcresulthandlers;import org.springframework.test.web.servlet.setup.mockmvcbuilders;import Org.springframework.web.context.webapplicationcontext;import com.controller.user.usercontroller;import com.entity.user.user;import static org.springframework.test.web.servlet.request.mockmvcrequestbuilders.*;import static org.springframework.test.web.servlet.result.mockmvcresultmatchers.*;import static org.springframework.test.web.servlet.result.mockmvcresulthandlers.*;import static org.springframework.test.web.servlet.setup.mockmvcbuilders.*;import static org.hamcrest.matchers.*;@ Runwith (Springjunit4classrunner.class) @ContextConfiguration (locations={"classpath:/spring/ Spring.applicationContext.mysql.xml "}) Public class testusercontroller {private mockmvc mockMvc; @Beforepublic void setup () {this.mockMvc = Standalonesetup (New usercontroller ()) .defaultrequest (Get ("/") .accept (Mediatype.application_json)) .alwaysexpect (Status (). IsOk ()) .alwaysexpect (content (). ContentType ("ApplicaTion/json;charset=utf-8 ")) .build (); @Testpublic void testdisplayuser () throws exception {this.mockmvc.perform (Get ("/users "). Andexpect (View (). Name (" Userlistview ")). Andexpect (Model (). Attribute (" Users ", Hassize (2)). Andexpect (Model (). Attribute ("Users", Hasitem (Hasproperty ("No", is ("FF0478")))). Andexpect (Model (). Attribute ("Users", Hasitem ( Hasproperty ("No", is ("FF0479")))). Anddo (print ());} @Testpublic void getuser () throws exception{this.mockmvc.perform (Get ("/users/5476")). Andexpect (model (). Attribute ("User", Hasproperty ("No", is ("FF0830"))). Andexpect (Model (). Attribute ("User", hasproperty ("id", is (5476))));} @Testpublic void saveuser () throws exception{this.mockmvc.perform (Get ("/user/save"). Param ( "Name", "Li Yang"). Param ("No", "FF0833"). Param ("password", "123"). Param ("DepartmentID", "101")) . Andexpect (View (). Name ("UserList"));} @Testpublic  VOID&NBsp;updateuser () throws exception {this.mockmvc.perform (Get ("/users/update"). PARAM ("id", " Param ("name", "Li Yang"). Param ("No", "FF0834"). Andexpect (View (). Name ("UserList"));}}
Test Demo for basic Spring test framework