junit测试控制层

◇◆丶佛笑我妖孽 提交于 2019-12-02 11:40:25
package com.ficus.face.product.spring.tutorial.score.controller;
import com.mongodb.util.JSON;
import net.minidev.json.JSONObject;
import org.junit.Assert;
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.http.MediaType;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.ResultActions;
import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultHandlers;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

@RunWith(SpringRunner.class)
@SpringBootTest
@WebAppConfiguration
public class CourseControllerTest {
    private MockMvc mockMvc;
    @Autowired
    private WebApplicationContext wac;
    @Before
    public void setUp() throws Exception{
        mockMvc = MockMvcBuilders.webAppContextSetup(wac).build();  //构建MockMVC
    }

    public MockHttpServletRequestBuilder getBuilder(String method, String url) throws Exception {
        MockHttpServletRequestBuilder builder= null;
       switch(method){
           case "post":    builder =MockMvcRequestBuilders.post(url); break;
           case  "delete": builder =MockMvcRequestBuilders.delete(url); break;
           case  "put" :   builder = MockMvcRequestBuilders.put(url); break;
           default: throw new Exception("非法请求方式");
        }

       return builder;
    }

    public Map request(String method, String url, Map map ) throws Exception {
        MockHttpServletRequestBuilder builder  = getBuilder(method,url);
        MvcResult mvcResult =  mockMvc.perform(builder
                .contentType(MediaType.APPLICATION_JSON)
                .content(JSONObject.toJSONString(map))
                .accept(MediaType.APPLICATION_JSON))
                .andExpect(MockMvcResultMatchers.status().isOk())
                .andDo(MockMvcResultHandlers.print())
                .andReturn();
        String result = mvcResult.getResponse().getContentAsString();
        Map maps = (Map)JSON.parse(result);
        return maps;
    }


    @Test
    public void queryScoreByStudentTest() throws Exception{
        Map<String, Object> map = new HashMap<>();
        map.put("id", "112");
//        MvcResult mvcResult =  mockMvc.perform(MockMvcRequestBuilders.post("/education/yitu/score_center/api/v1/course/query_score_by_course")
//                .contentType(MediaType.APPLICATION_JSON)
//                .content(JSONObject.toJSONString(map))
//                .accept(MediaType.APPLICATION_JSON))
//                .andExpect(MockMvcResultMatchers.status().isOk())
//                .andDo(MockMvcResultHandlers.print())
//                .andReturn();
//        String result = mvcResult.getResponse().getContentAsString();
//        Map maps = (Map)JSON.parse(result);
         Map maps = request("post",
                 "/education/yitu/score_center/api/v1/course/query_score_by_course",
                 map);
        String rtn = (String)maps.get("rtn");
        Assert.assertEquals("0",rtn);
    }


}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!