spring boot 单元测试调用controller接口
程序员文章站
2022-04-29 16:38:43
...
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());
}
}
上一篇: Python--5 函数
推荐阅读
-
深入理解spring boot异步调用方式@Async
-
详解Spring Boot实战之Filter实现使用JWT进行接口认证
-
spring boot 2 集成JWT实现api接口认证
-
详解Spring Boot Junit单元测试
-
详解Spring Boot实战之单元测试
-
快速入门Spring Boot之开发你的第一个JSON接口
-
idea创建一个入门Spring Boot项目(controller层)使用Moven代码管理
-
Spring-Boot ☞ ShapeFile文件读写工具类+接口调用
-
Spring Boot + Redis 实现接口幂等性
-
Spring Boot通过Junit实现单元测试过程解析