当前位置: 首页 > news >正文

SpringBoot:使用spring-boot-test对web应用做单元测试时如何测试Filter?

对SpringBoot的Web应用做单元测试时,一般会使用spring-boot-test,pom.xml中会添加如下内容:

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
 			<scope>test</scope>
		</dependency>

代码举例如下:

import static org.junit.jupiter.api.Assertions.assertEquals;

import javax.annotation.Resource;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.HttpHeaders;
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.MockHttpServletRequestBuilder;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import org.springframework.test.web.servlet.setup.DefaultMockMvcBuilder;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;

import com.test.protobuf.App;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = App.class)
public class WebAppTester {

	@Resource
	private WebApplicationContext ctx = null;

	private MockMvc mockMvc = null;

	@Before
	public void setupMockMvc() throws Exception {
		DefaultMockMvcBuilder builder = MockMvcBuilders.webAppContextSetup(ctx);

		this.mockMvc = builder.build();
	}

	@Test
	public void test01() throws Exception {
		String result = doHttpAsyncTest(mockMvc, "/admin/health", "", null);

		assertEquals("ok", result);
	}

	public String doHttpAsyncTest(MockMvc mockMvc, String uri, String httpBody, HttpHeaders httpHeaders)
			throws Exception {

		MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.post(uri)
				.contentType(MediaType.APPLICATION_JSON);

		if (httpHeaders != null) {
			builder.headers(httpHeaders);
		}

		MvcResult result = mockMvc.perform(builder.content(httpBody)).andReturn();
		String httpRespBody = mockMvc.perform(MockMvcRequestBuilders.asyncDispatch(result))
				.andExpect(MockMvcResultMatchers.status().isOk())
				.andExpect(MockMvcResultMatchers.content().contentType(MediaType.APPLICATION_JSON)).andReturn()
				.getResponse().getContentAsString();

		return httpRespBody;
	}

}

如果应用中以如下方式添加了Filter的话,使用以上代码无法测试这些Filter:

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class TestFilter implements Filter {

	private String checkUrl = "/";

	@Override
	public void init(FilterConfig config) throws ServletException {
		String checkUrl = config.getInitParameter("checkUrl");

		if (checkUrl != null) {
			this.checkUrl = checkUrl;
		}
	}

	@Override
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
			throws IOException, ServletException {

		HttpServletRequest httpReqt = (HttpServletRequest) request;
		String reqtUrl = httpReqt.getRequestURI();

		if (!reqtUrl.startsWith(checkUrl)) {
			chain.doFilter(request, response);

			return;
		}

		HttpServletResponse httpResp = (HttpServletResponse) response;
		httpResp.setStatus(200);
		httpResp.setContentType("text/plain");
		httpResp.getWriter().write("ok");

		return;
	}

}
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class FilterRegistrationConfig {

	public FilterRegistrationBean<TestFilter> testFilter() {
		FilterRegistrationBean<TestFilter> reg = new FilterRegistrationBean<TestFilter>();

		reg.setFilter(new TestFilter());
		reg.addInitParameter("checkUrl", "/admin/");
		reg.setName("TestFilter");

		return reg;
	}
}

如果想把这些Filter也测试到,需要在setupMockMvc方法中将Filter注册并初始化一下,代码如下:

import com.test.protobuf.App;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = App.class)
public class WebAppTester {

	@Resource
	private WebApplicationContext ctx = null;

	private MockMvc mockMvc = null;

	@Resource
	private FilterRegistrationBean<?>[] filterRegistrationBean = null;

	@Before
	public void setupMockMvc() throws Exception {
		DefaultMockMvcBuilder builder = MockMvcBuilders.webAppContextSetup(ctx);

		for (FilterRegistrationBean<?> bean : filterRegistrationBean) {

			MockFilterConfig filterConfig = new MockFilterConfig();
			for (Entry<String, String> params : bean.getInitParameters().entrySet()) {
				filterConfig.addInitParameter(params.getKey(), params.getValue());
			}

			Filter filter = bean.getFilter();
			filter.init(filterConfig);

			builder.addFilter(filter, bean.getUrlPatterns().toArray(new String[0]));
		}

		this.mockMvc = builder.build();
	}
......
}

相关文章:

  • Redis 集群相关知识介绍
  • 小初高各学科教材,PDF电子版下载
  • 切换git仓库远程地址
  • C#windows窗体人脸识别
  • Ubuntu 下 nginx-1.24.0 源码分析 - ngx_atomic_cmp_set 函数
  • Django5的新特征
  • Manga Image Translator 开源 AI 漫画翻译工具的深度剖析
  • 【编写UI自动化测试集】Appium+Python+Unittest+HTMLRunner​
  • ubuntu服务器 如何配置安全加固措施
  • Visual Studio 使用 “Ctrl + /”键设置注释和取消注释
  • 肝了半年,我整理出了这篇云计算学习路线(新手必备,从入门到精通)
  • 安卓手游内存call综合工具/内部call/安卓注入call/数据分析(类人猿学院)
  • C语言-章节 4:函数的定义与声明 ——「神秘法术的卷轴」
  • stm32mp集成swupdateOTA升级
  • docker nginx 配置文件详解
  • vuedraggable固定某一item的记录
  • CentOS安装Docker,Ubuntu安装Docker,Docker解决方案
  • 001 SpringCloudAlibaba整合 - Nacos注册配置中心、Sentinel流控、Zipkin链路追踪、Admin监控
  • LeapMotion第2代 Unity示范代码(桌面开发)
  • 快速幂(算法)的原理
  • 杨国荣︱《老子智慧八十一讲》及其他
  • 龚正盛秋平王晓真共同启动2025国际消费季暨第六届上海“五五购物节”
  • 特朗普称加总理将很快访美,白宫:不影响将加拿大打造成“第51个州”计划
  • 神十九乘组安全顺利出舱
  • 宋徽宗《芙蓉锦鸡图》亮相,故宫首展历代动物绘画
  • “乐购浦东”消费券明起发放,多个商家同期推出折扣促销活动