Spring-boot-starter-test

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.