1. Conventions
The unit test code is written in the Src/test/java directory
The Unit test class is named *test, prefixed with the class name to be tested
2. Unit test using mock mode
The Spring test Framework provides MOCKMVC objects that can be tested in MVC without the need for client-server requests, and can execute the controller's request completely on the server side, just as it does with the test servers.
The test environment needs to be established before the test starts, and the Setup method is @before modified. Use the Mockmvcbuilders tool to create a Mockmvc object using the Webapplicationcontext object as a parameter.
@RunWith (Springrunner.Class@SpringBootTest (classes = Application.ClassThe application here is Springboot's startup class name.@WebAppConfigurationPublicClassstylecontrollertest {@AutowiredPrivate Webapplicationcontext context;Private MOCKMVC Mockmvc;Private Objectmapper mapper =new Objectmapper (); @Before public void Setupmockmvc () throws Exception {Mockmvc = Mockmvcbuilders.webappcontextsetup ( context). build (); } @Test public void Testsend () throws Exception {Long id =1l; //Call interface, pass in the added user parameter Mockmvc.perform (mockmvcrequestbuilders.get ("/style/liststylebyid"). ContentType ( MEDIATYPE.APPLICATION_JSON_UTF8). Content (mapper.writevalueasstring (ID))). Andexpect ( Mockmvcresultmatchers.status (). IsOk ()). Andexpect (Mockmvcresultmatchers.content (). ContentType ( Mediatype.application_json_utf8). Anddo (Mockmvcresulthandlers.print ()); }}
3. Using Feign Mode Unit test
The following is a sample unit test for the feign interface, starting the project, testing the service provided by this jar, not starting the service, changing to a remote service address, and testing the services provided by the remote Jar
which
@EnableFeignClients(clients = UserControllerTest.UserServiceFeignClient.class)
Similar to our actual application invocation of related services.
@RunWith (Springjunit4classrunner.Class@SpringBootTest (classes = usercontrollertest.Class@Import ({feignautoconfiguration.Class, Httpmessageconvertersautoconfiguration.Class})@EnableFeignClients (clients = usercontrollertest.userservicefeignclient.Classpublic class usercontrollertest { @FeignClient (value = "loan-server", url = "http://localhost:9070/") public interface Userservicefeignclient extends userserviceclient {} @Autowired private userservicefeignclient userservicefeignclient; @Test public void GetUser () {User user = Userservicefeignclient.getsdkuserbyid (1); SYSTEM.OUT.PRINTLN (user); }}
4. Using the HTTP Rest API Unit test
Use Resttemplate to initiate a GET or POST request where @springboottest the two lines comment out without launching the Springboot container for direct remote call testing
@RunWith (Springjunit4classrunner.ClassPublicclass loancontrollertest {private final static String url = private static resttemplate resttemplate = new resttemplate (); @Test public void Test () { responseentity<string> response = resttemplate.exchange (url + "/loan/getloanbyid?id =1 ", Httpmethod.get, new httpentity (null), String.class); System.out.println ( "Result:" + response.getbody ())}}
Spring-boot-starter-test