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

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

XML 复制代码
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
 			<scope>test</scope>
		</dependency>

代码举例如下:

java 复制代码
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:

java 复制代码
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;
	}

}
java 复制代码
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注册并初始化一下,代码如下:

java 复制代码
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();
	}
......
}
相关推荐
m0_7482552634 分钟前
Spring Boot 3.x 引入springdoc-openapi (内置Swagger UI、webmvc-api)
spring boot·后端·ui
川石课堂软件测试1 小时前
涨薪技术|Kubernetes(k8s)之Ingress
功能测试·云原生·容器·kubernetes·单元测试
m0_748238272 小时前
SpringBoot 如何调用 WebService 接口
java·spring boot·后端
小王不会写code2 小时前
spring boot+vue项目(免费)
java·spring boot·后端
猿来入此小猿3 小时前
基于SpringBoot实现旅游酒店平台功能六
java·spring boot·毕业设计·旅游·毕业源码·免费学习·旅游景点
m0_748229993 小时前
Spring Boot(十六):使用 Jenkins 部署 Spring Boot
spring boot·后端·jenkins
猿来入此小猿3 小时前
基于SpringBoot实现旅游酒店平台功能三
spring boot·毕业设计·旅游·猿来入此·旅游景点·旅游酒店·旅游pigtail
caihuayuan54 小时前
「mysql」Mac mysql一路畅通式安装
java·大数据·spring boot·后端·课程设计
蜗牛 | ICU5 小时前
【推荐项目】 043-停车管理系统
java·vue.js·spring boot·前端框架
MrWho不迷糊5 小时前
Spring Batch 多写:分类写与组合写
spring boot·后端