spring boot 单元测试调用controller接口

package com.inco;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;

import javax.annotation.Resource;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = IncoDemoApplication.class)
public class ControllerTests {
    /**
     *  注入一个web应用环境(容器)
     */
    @Resource
    private WebApplicationContext webApplicationContext;
    //mvc 环境对象
    private MockMvc mockMvc;
    @Before
    public void init(){
        mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
    }
    @Test
    public void findObject() throws Exception {
        MvcResult result = mockMvc.perform(MockMvcRequestBuilders.post("/jcsz/queryJcList").param("pxn","2019-2020").param("pxq","2")) .andReturn(); //返回MvcResult
        System.out.println(result.getResponse().getContentAsString());

    }
}

你可能感兴趣的:(Spring2.0,spring,boot,单元测试,java)