SpringBoot Mock测试RequestBody参数并包含其他参数接口
2021-04-14 21:27
标签:使用 span bool 函数 失败 subject demo1 number utf8 (当接口的参数用@RequestBody修饰,同时还有另外的参数的情况) 测试接口的时候,如果项目中请求经过网关,转发到服务时,中间会将请求头数据转换成参数对象Subject。 格式如下: 这时,如果使用mock测试,参数subject传递一直失败,无法解析。 经过一番查找,将实现了解析参数的 WebMvcConfigurer的实现类WebConfiguration加到Test的基类中加载,实现了正常的接口测试 请求参数与接口参数匹配类 请求拦截器GlobalContextFilter 测试基类 Controller测试类 SpringBoot Mock测试RequestBody参数并包含其他参数接口 标签:使用 span bool 函数 失败 subject demo1 number utf8 原文地址:https://www.cnblogs.com/AwenDF/p/13336278.html@PutMapping("/demo/update")
public String update(@Valid @RequestBody DemoRO demoRO, Subject subject) {
//...
return "success";
}@Configuration
public class WebConfiguration implements WebMvcConfigurer {
public WebConfiguration() {
}
//...
@Bean
public FilterRegistrationBean
public void addArgumentResolvers(Listpublic class SubjectHandlerMethodArgumentResolver implements HandlerMethodArgumentResolver {
public SubjectHandlerMethodArgumentResolver() {
}
public boolean supportsParameter(MethodParameter parameter) {
return Subject.class.isAssignableFrom(parameter.getParameterType());
}
public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {
//如果接口参数满足类型匹配(即 supportsParameter == true),则获取由GlobalContext管理的存储在ThreadLocal的subject
return GlobalContext.getSubject().orElse((Object)null);
}
}public class GlobalContextFilter extends OncePerRequestFilter {
public GlobalContextFilter() {
}
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
this.resolverSubject(request);
try {
filterChain.doFilter(request, response);
} finally {
this.clear();
}
}
private void clear() {
GlobalContext.clear();
}
//...
private void resolverSubject(HttpServletRequest request) {
if (NumberUtils.isNumber(request.getHeader("user-Id"))) {
Long id = Long.valueOf(request.getHeader("user-Id"));
Subject subject = Subject.builder().id(id).build();
//解析请求头,并存入封装了ThreadLocal的GlobalContext中
GlobalContext.setSubject(subject);
}
}
}
@RunWith(SpringRunner.class)
@SpringBootTest(classes = DemoApplication.class, webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
@ContextConfiguration(classes = WebConfiguration.class, initializers= ConfigFileApplicationContextInitializer.class)
@AutoConfigureMockMvc
public class BaseTest {
@Autowired
protected MockMvc mockMvc;
/**
* web项目上下文
*/
@Resource
private WebApplicationContext webApplicationContext;
@Resource
private FilterRegistrationBean
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
public class DemoControllerTest extends BaseTest {
private final HttpHeaders httpHeaders = new HttpHeaders();
@Override
public void before() {
super.before();
httpHeaders.add("user-Id", "100001");
}
@Test
public void testUpdate() throws Exception {
DemoRO updateRO = new DemoRO();
updateRO.setName("Demo111");this.mockMvc.perform(
put("/demo/update")
.contentType(MediaType.APPLICATION_JSON_UTF8)
.headers(httpHeaders)
.content(JSON.toJSONString(updateRO))
).andDo(print());
}
文章标题:SpringBoot Mock测试RequestBody参数并包含其他参数接口
文章链接:http://soscw.com/index.php/essay/75817.html