springboot MockMvc单元测试

什么是MockMvc?参考:https://blog.csdn.net/kqzhu/article/details/78836275

直接贴代码了:

package demoTest;

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import java.io.Serializable;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;

import api.controller.Application;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = {Application.class},webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)

public class APITest implements Serializable{
	private static final long serialVersionUID = 2585236388671661262L;

	private MockMvc mockMvc;
	
	@Autowired
	private WebApplicationContext webApplicationContext;
	
	@Before  
        public void setup(){
	  mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build(); 
	 }
	
	
	@Test
	public void queryAllEmployee() throws Exception{
		this.mockMvc
		.perform(get("/employee/query/all"))
		.andExpect(status().isOk())
                .andDo(print())
                .andReturn();

	}
	

	@Test
	public void addSingleEmployee() throws Exception{
		EmployeeBody employeeBody=new EmployeeBody();
		employeeBody.setName("t");
		employeeBody.setEmail("[email protected]");
		employeeBody.setGender("man");
		employeeBody.setPhonenum("199");
		employeeBody.setAge(21);
		
		ObjectMapper mm = new ObjectMapper();
		String requestJson = mm.writeValueAsString(employeeBody);
		
		this.mockMvc
				 .perform(post("/employee/add/single")
				 .contentType(MediaType.APPLICATION_JSON)
				 .content(requestJson))
				 .andExpect(status().isOk())
				 .andDo(print())
				 .andReturn();
	}
	
	
}

 运行queryAllEmployee()后打印的结果:

MockHttpServletRequest:
      HTTP Method = GET
      Request URI = /employee/query/all
       Parameters = {}
          Headers = {}

Handler:
             Type = api.controller.EmployeeController
           Method = public entity.MultipleEmployeeResponse api.controller.EmployeeController.queryAllEmployees()

Async:
    Async started = false
     Async result = null

Resolved Exception:
             Type = null

ModelAndView:
        View name = null
             View = null
            Model = null

FlashMap:
       Attributes = null

MockHttpServletResponse:
           Status = 200
    Error message = null
          Headers = {Content-Type=[application/json;charset=UTF-8]}
     Content type = application/json;charset=UTF-8
             Body = {"responseResult":{"result":"successful","message":null,"timeStamp":"2018-09-07T11:22:06Z"},"employees":[{"name":"a","email":"[email protected]","gender":"man","phonenum":"139","age":23},{"name":"b","email":"[email protected]","gender":"woman","phonenum":"136","age":20},{"name":"c","email":"[email protected]","gender":"man","phonenum":"136","age":21},{"name":"d","email":"[email protected]","gender":"man","phonenum":"136","age":21}]}
    Forwarded URL = null
   Redirected URL = null
          Cookies = []

猜你喜欢

转载自blog.csdn.net/yicai168/article/details/82494208