Spring Boot MockMvc objects using analog calls Controller

  After the function realization, we should cultivate the habit of hand written supporting unit testing, which is particularly important in the micro-services architecture. Normally, when we implement micro-services architecture has been achieved before and after the end of the separation of infrastructure projects and deployment. So when implementing the back-end services, unit test code that is used to verify the correctness of very good means during development and testing of these units will be well supported by the reconstruction of our future might be.

 

  Write controller

package com.xc.springboot.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {

    /**
     * http://127.0.0.1:8081/hello
     */
    @RequestMapping("/hello")
    public String hello() {
        return "hello world!";
    }

}

 

  Write test classes under src / test /

package com.xc.springboot.controller;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.http.MediaType;
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.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;

//引入下面的静态引用,让status、content、equalTo函数可用
import staticorg.hamcrest.Matchers.equalTo;
 Import  static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
 Import  static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; 

@RunWith (the SpringJUnit4ClassRunner. class ) // introduced support for JUnit4 of Spring.
// @SpringApplicationConfiguration (classes = SpringbootApplication.class) // specifies the Spring Boot start classes. 
@WebAppConfiguration // open configuration of Web applications, for analog ServletContext. 
public  class HelloControllerTest { 

    Private MockMvc MVC; 

    //· @ Before: JUnit test cases defined before execution @Test content preloaded content, initialize simulation HelloController here for. 
    @Before
     public  void the setUp () throws Exception { 
        MVC = MockMvcBuilders.standaloneSetup ( new new HelloController in ()) Build ();. 
    } 

    @Test 
    public  void Hello () throws Exception { 

        // · MockMvc objects: Analog Interface Controller calling initiate a request, in the hello test @Test defined, perform function performs a call request, for performing Accept type of the received data, andExpect expected value for determining the returned interface. 
        mvc.perform (MockMvcRequestBuilders.get ( "/ Hello" ) .accept (MediaType.APPLICATION_JSON))  
                .andExpect (Status (). ISOK ())
                .andExpect (Content () String (equalTo (. "Hello World!" )));
    }


}

 

  Code is interpreted as follows.

  · @ RunWith (SpringJUnit4ClassRunner.class): Spring's support for the introduction of JUnit4.

  · @ SpringApplicationConfiguration (classes = SpringbootApplication.class): Specifies the Spring Boot start classes.

  · @ WebAppConfiguration: open the Configure Web application for simulation ServletContext.

  · MockMvc objects: Analog Interface Controller call initiation request, the test case in the hello @Test defined, Perform function performs a call request, for performing Accept type of the received data, andExpect expected value for determining the returned interface.

  · @ Before: JUnit test cases defined before execution @Test content preloaded content, initialize simulation HelloController here for.

  Note the introduction of the following static reference, so that status, content, equalTo functions are available:

import static org.hamcrest.Matchers.equalTo;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

 

版本:spring-boot-starter-parent 1.5.21.RELEASE

Reference: Spring Cloud microService combat p51

 

Guess you like

Origin www.cnblogs.com/ooo0/p/11220458.html