Open CharLemAznable opened 5 years ago
@EnableWebMvc // IMPORTANT
@ComponentScan
@Configuration
public class YourConfiguration implements WebMvcConfigurer {
@Autowired
private YourInterceptor yourInterceptor;
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(yourInterceptor).addPathPatterns("/**");
}
}
private static MockMvc mockMvc;
@Autowired
private WebApplicationContext webApplicationContext;
@BeforeAll
public void setup() {
mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext)
.addFilters(yourFilter).build();
}
添加maven依赖
单元测试类添加注解
初始化MockMvc
单元测试用例