【问题标题】:Testing onErrorResume() Spring Webflux测试 onErrorResume() Spring Webflux
【发布时间】:2021-11-23 19:16:14
【问题描述】:

我有一个使用 Spring Webflux 和反应器的服务层,我正在为此编写单元测试。我能够测试良好的响应场景,但不确定如何使用 StepVerifier 测试onErrorResume()。另外请告诉我是否有更好的方法来处理我的控制器中的异常(例如:使用switchIfEmpty()

这是我的控制器方法

    public Mono<SomeType> getInfo(Integer id) {
            return webClient
                    .get()
                    .uri(uriBuilder -> uriBuilder.path())
                    .header("", "")
                    .header("", "")
                    .header("", "")
                    .header(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON_VALUE)
                    .retrieve()
                    .bodyToMono(POJO.class)
                    .onErrorResume(ex -> {
                        if (ex instanceof WebFaultException)
                            return Mono.error(ex);
                        return Mono.error(new WebFaultException(ex.getMessage(), "Error on API Call", HttpStatus.INTERNAL_SERVER_ERROR));
                    });
        }
    }

【问题讨论】:

    标签: java unit-testing mockito spring-webflux project-reactor


    【解决方案1】:

    您可以模拟 webclient 并在调用 webclientMock.get() 时使用 Mockito.doThrow

    
    YourWebClient webclientMock = mock(YourWebClient.class);
    
    
    doThrow(RuntimeException.class)
          .when(webclientMock)
          .get();
    
    // Call your method here
    
     Exception exception = assertThrows(RuntimeException.class, () -> {
            YourController.getInfo(someIntValue);
        });
    
    // If you chose to raise WebFaultException, addittionaly assert that the return values ( message, status) are the one you expected
    
    

    【讨论】:

      【解决方案2】:

      另一种测试您的WebClient 代码而不必模拟WebClient 类本身的方法是使用ExchangeFunction 构建您的WebClient,该ExchangeFunction 返回任何响应或错误你期望。我发现这是在模拟 WebClient 和启动 Wiremock 进行单元测试之间的一种愉快的媒介。

          @Test
          void ourTest() {
              ExchangeFunction exchangeFunction = mock(ExchangeFunction.class);
              // this can be altered to return either happy or unhappy responses        
              given(exchangeFunction.exchange(any(ClientRequest.class))).willReturn(Mono.error(new RuntimeException()));        
              WebClient webClient = WebClient.builder()
                      .exchangeFunction(exchangeFunction)
                      .build();
      
              // code under test - this should live in your service
              Mono<String> mono = webClient.get()
                      .uri("http://someUrl.com")
                      .retrieve()
                      .bodyToMono(POJO.class)
                      .onErrorResume(ex -> {
                          if (ex instanceof WebFaultException)
                              return Mono.error(ex);
                          return Mono.error(new WebFaultException(ex.getMessage(), "Error on API Call", HttpStatus.INTERNAL_SERVER_ERROR));
                      });
      
              StepVerifier.create(mono)
                      .expectError(RuntimeException.class)
                      .verify();
          }
      
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-05-18
        • 2019-01-20
        • 2019-01-20
        • 2021-08-23
        • 2019-06-27
        • 2020-12-31
        • 1970-01-01
        相关资源
        最近更新 更多