如何测试Spring Boot处理程序拦截器

时间:2017-10-02 18:09:08

标签: java spring spring-mvc spring-boot spring-boot-test

我们正在尝试使用spring boot version 1.4.0在我们的spring boot应用程序中对拦截器进行集成测试,但不确定如何;这是我们的应用程序设置

@Configuration
@EnableAutoConfiguration()
@ComponentScan
public class Application extends SpringBootServletInitializer {
  @Override
  protected SpringApplicationBuilderconfigure(SpringApplicationBuilder application) {
  return application.sources(Application.class);
}

然后我们通过扩展WebMvcConfigurerAdapter

来定制webmvc
@Configuration
public class CustomServletContext extends WebMvcConfigurerAdapter{
  @Override
  public void addInterceptors(InterceptorRegistry registry) {registry.addInterceptor(testInterceptor).addPathPatterns("/testapi/**");
  }
}

所以我们想测试拦截器,但我们不想真正启动应用程序,因为有许多依赖bean需要读取外部定义的属性文件来构建

我们尝试了以下

@SpringBootTest(classes = CustomServletContext.class)
@RunWith(SpringRunner.class)
public class CustomServletContextTest {

  @Autowired
  private ApplicationContext applicationContext;

  @Test
  public void interceptor_request_all() throws Exception {
    RequestMappingHandlerMapping mapping = (RequestMappingHandlerMapping) applicationContext
        .getBean("requestMappingHandlerMapping");
    assertNotNull(mapping);

    MockHttpServletRequest request = new MockHttpServletRequest("GET",
        "/test");

    HandlerExecutionChain chain = mapping.getHandler(request);

    Optional<TestInterceptor> containsHandler = FluentIterable
        .from(Arrays.asList(chain.getInterceptors()))
        .filter(TestInterceptor.class).first();

    assertTrue(containsHandler.isPresent());
  }
}

但它改变了org.springframework.beans.factory.NoSuchBeanDefinitionException:没有名为&#39; requestMappingHandlerMapping&#39;已定义

我们是否需要创建一个requestMappingHandlerMapping的bean来测试拦截器?春季靴子有没有神奇的方法呢?

1 个答案:

答案 0 :(得分:1)

您可以创建这样的测试:

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = { MyIncludedConfig.class })
@ActiveProfiles("my_enabled_profile")
public class BfmSecurityInterceptorTest2 {

    public static final String TEST_URI = "/test";
    public static final String RESPONSE = "test";

    // this way you can provide any beans missing due to limiting the application configuration scope
    @MockBean
    private DataSource dataSource;

    @Autowired
    private TestRestTemplate testRestTemplate;

    @Test
    public void testInterceptor_Session_cookie_present_Authorized() throws Exception {

        ResponseEntity<String> responseEntity = testRestTemplate.getForEntity(TEST_URI, String.class);

        assertThat(responseEntity.getStatusCode()).isEqualTo(HttpStatus.OK);
        assertThat(responseEntity.getBody()).isEqualTo(RESPONSE);

    }

    @SpringBootApplication
    @RestController
    public static class TestApplication {

        @GetMapping(TEST_URI)
        public String test() {
            return RESPONSE;
        }

    }

}

注释

  • 仅当您设置SpringBootTest.WebEnvironment.RANDOM_PORT时,拦截器才起作用
  • 您必须提供足够的配置,以便执行拦截器
  • 要加快测试速度,您可以排除不需要的bean和配置,请参见示例