spring boot controller 单元测试

package com.example.demo;


import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
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.MvcResult;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.context.WebApplicationContext;


@RunWith(SpringRunner.class)
@SpringBootTest
public class DemoApplicationTests {



    @Autowired
    private WebApplicationContext context;

    private MockMvc mockMvc;



    @Before
    public void setupMockMvc() {
        mockMvc = MockMvcBuilders.webAppContextSetup(context).build();
    }

    @Test
    public void contextLoads() throws Exception {
        MultiValueMap<String, String> params = new LinkedMultiValueMap<String, String>();
//        params.add("id", "10");

        postTest(params,"/as");
    }


    /**
     * get 请求方式
     * @param params
     * @param path
     * @return
     * @throws Exception
     */
    public  Object getTest(MultiValueMap<String, String> params,String path) throws Exception {
        MvcResult result =null;
        try {
            System.out.println("传入参数:"+params);
            if (params==null||params.isEmpty()){
                result = mockMvc.perform(get(path))
                        .andExpect(status().isOk())
                        .andReturn();
            }else {
                result = mockMvc.perform(get(path).params(params))
                        .andExpect(status().isOk())
                        .andReturn();
            }
        } catch (java.lang.AssertionError e) {
            erro(e.getMessage());
            System.exit(1);
        }
        System.out.println("返回参数:"+result.getResponse().getContentAsString());
        return result.getResponse().getContentAsString();
    }

    /**
     * post 请求方式
     * @param params
     * @param path
     * @return
     * @throws Exception
     */
    public  Object postTest(MultiValueMap<String, String> params,String path) throws Exception {
        MvcResult result =null;
        try {
            System.out.println("传入参数:"+params);
            if (params==null||params.isEmpty()){
                result = mockMvc.perform(post(path))
                        .andExpect(status().isOk())
                        .andReturn();
            }else {
                result = mockMvc.perform(post(path).params(params))
                        .andExpect(status().isOk())
                        .andReturn();
            }
        } catch (java.lang.AssertionError e) {
            erro(e.getMessage());
            System.exit(1);
        }
        System.out.println("返回参数:"+result.getResponse().getContentAsString());
        return result.getResponse().getContentAsString();
    }

    /**
     * 异常类型判断
     * @param erro
     */
    public void erro(String erro){
        if (erro.contains("404")){
            System.out.println("请求url异常:"+404);
        }else if (erro.contains("405")){
            System.out.println("请求方式异常:"+405);
        }else {
            System.out.println("未编辑异常:"+erro);
        }
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_43113795/article/details/84921856