首页 > 解决方案 > 在 HttpServletRequest 上下文之外测试 Spring Boot WebClient

问题描述

我的 Spring boot 2.5 应用程序是一个相当标准的 Web 应用程序,它公开一些端点并调用其他服务。在通常的用例中,外部请求进来,并应用一些业务逻辑,这将调用其他服务,使用配置了 Oauth2 的 WebClient。

但是,我的应用程序还包含一个计划任务,该任务需要对其他服务进行一些调用。

这就是问题开始的地方:我相信我面临的问题与Spring Security 5 Calling OAuth2 Secured API in Application Runner 会导致 IllegalArgumentException相同,即servletRequest cannot be null在非 Servlet 环境中使用 webClient 时。

我以为我可以通过集成测试轻松重现这一点,但我不是。我已经有一个SpringBootTest计划任务,我在其中加载 Spring 上下文,访问具有逻辑的类,并简单地调用在 prod 中部署时计划的方法......但它没有失败:我没有能够繁殖。

当我调试测试时,我看到运行测试时 servletRequest 确实不为空(当它在 prod 中运行时):有一个 Spring MockhttpServletrequest,所以我没有与 prod 中相同的问题。

在此处输入图像描述

我试图在完全没有“网络”支持的情况下运行测试,使用

@SpringBootTest(webEnvironment = WebEnvironment.NONE) 

但是当我这样做时,我的应用程序启动时缺少一些必需的元素:我在我的应用程序中配置了 2 个 webClients,一个是“正常”的,一个是我计划用于调度程序的一个:

  @Bean
  @Primary
  WebClient servletWebClient(ClientRegistrationRepository clientRegistrations,
                             OAuth2AuthorizedClientRepository authorizedClients) {

    //this constructor  will configure internally a RemoveAuthorizedClientOAuth2AuthorizationFailureHandler,
    // and onAuthorizationFailure will be called on it when we get a 401
    var oauth = new ServletOAuth2AuthorizedClientExchangeFilterFunction(clientRegistrations, authorizedClients);

    oauth.setDefaultClientRegistrationId("keycloak");


    return WebClient.builder()
        .defaultHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
        .apply(oauth.oauth2Configuration())
        .build();
  }

  @Bean
  @Qualifier("forScheduler")
  WebClient schedulerWebClient(OAuth2AuthorizedClientManager authorizedClientManager) {

    ServletOAuth2AuthorizedClientExchangeFilterFunction oauth =new ServletOAuth2AuthorizedClientExchangeFilterFunction(authorizedClientManager);

    oauth.setDefaultClientRegistrationId("keycloak");

    return WebClient.builder()
        .defaultHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
        .apply(oauth.oauth2Configuration())
        .build();
  }

  @Bean
  public OAuth2AuthorizedClientManager authorizedClientManagerForScheduler(
      ClientRegistrationRepository clientRegistrationRepository,
      OAuth2AuthorizedClientService clientService)
  {

    OAuth2AuthorizedClientProvider authorizedClientProvider =
        OAuth2AuthorizedClientProviderBuilder.builder()
            .clientCredentials()
            .build();

    AuthorizedClientServiceOAuth2AuthorizedClientManager authorizedClientManager =
        new AuthorizedClientServiceOAuth2AuthorizedClientManager(
            clientRegistrationRepository, clientService);
    authorizedClientManager.setAuthorizedClientProvider(authorizedClientProvider);

    return authorizedClientManager;
  }

但是当使用 配置我的测试时webEnvironment = WebEnvironment.NONE, noClientRegistrationRepository​​被加载,并且声明的servletWebClient不能被实例化,因为它是它的依赖项之一。

手动测试这个非常困难/昂贵,所以我真的很想通过我的集成测试获得某种验证,在我部署它之前,我对 2 个 webClients 的更改将起作用。

如何正确测试该场景?或者换句话说,有没有办法有一个默认的 Spring Boot 测试,但没有一个 mockServletContext 被 Spring 初始化?

标签: javaspring-bootspring-webclient

解决方案


我使用自定义 TestExecutionListener 找到了采用不同方法的解决方案。

https://stackoverflow.com/a/69995757/3067542中的更多详细信息


推荐阅读