欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

spring 单元测试之controller 层

程序员文章站 2022-03-02 18:06:13
...

测试 Controller   POST       返回    JSON

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.test.context.ContextConfiguration;
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.MvcResult;
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 com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;


@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:applicationContext.xml", "classpath:spring-mvc.xml" })
@WebAppConfiguration
public class AdminTestControllerPost {

     protected MockMvc mockMvc;

     @Autowired
     protected WebApplicationContext wac;

     @Before // 这个方法在每个方法执行之前都会执行一遍
     public void setup() {
           mockMvc = MockMvcBuilders.webAppContextSetup(wac).build(); // 初始化MockMvc对象
     }

     @Test
     public void testGetSequence() {
           try {
                MvcResult mvcResult = mockMvc.perform(MockMvcRequestBuilders.post("/score"))
                          .andExpect(MockMvcResultMatchers.status().is(200)).andDo(MockMvcResultHandlers.print()).andReturn();
                int status = mvcResult.getResponse().getStatus();
                System.out.println("请求状态码:" + status);
                String result = mvcResult.getResponse().getContentAsString();
                System.out.println("接口返回结果:" + result);
                JSONObject resultObj = JSON.parseObject(result);
                // 判断接口返回json中success字段是否为true
                Assert.assertTrue(resultObj.getBooleanValue("success"));
           } catch (Exception e) {
                e.printStackTrace();
           }
     }
}

测试 Controller   GET    返回     JSON

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 org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.test.context.ContextConfiguration;
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.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:applicationContext.xml", "classpath:spring-mvc.xml" })
@WebAppConfiguration
public class AdminTestController {

     protected MockMvc mockMvc;

     @Autowired
     protected WebApplicationContext wac;

     @Before // 这个方法在每个方法执行之前都会执行一遍
     public void setup() {
           mockMvc = MockMvcBuilders.webAppContextSetup(wac).build(); // 初始化MockMvc对象
     }
     @Test
     public void getAllCategoryTest() throws Exception {
           String responseString = mockMvc.perform(get("/score") // 请求的url,请求的方法是get
                     // get("/user/showUser2") //请求的url,请求的方法是get
                     .contentType(MediaType.APPLICATION_FORM_URLENCODED)// 数据的格式
                     .param("page", "1") // 添加参数(可以添加多个)
           ).andExpect(status().isOk()) // 返回的状态是200
                     .andDo(print()) // 打印出请求和相应的内容
                     .andReturn().getResponse().getContentAsString(); // 将相应的数据转换为字符串
           System.out.println("-----返回的json = " + responseString);
           
     }
}