【问题标题】:Mono.doOnError() reactor block unit testMono.doOnError() 反应器块单元测试
【发布时间】:2019-08-26 14:40:58
【问题描述】:

我有一个使用 spring webflux 和 reactor 的休息控制器,我正在为控制器编写单元测试。请在下面找到代码 sn-ps 并帮助我编写单元测试方法来测试 .doOnError 块。

我尝试使用 Mockito 引发异常

doThrow(CriticalException.class) .when(myService).myMethod(object);

This is my unit test


StepVerifier.create(
Mono.just(
                webTestClient.post() 
                       .uri("/endpoint")
                       .accept(MediaType.APPLICATION_JSON) 


                       .body(BodyInserters.fromObject(requestJson)) //Set the body of the request to the given synchronous Object
                                            //Returns:a Mono with the response
        //Act
                       .exchange()  //Perform the exchange 
        //Assert        
                       .expectStatus().isOk() 
                       .expectBody(Agreement.class)
                       .returnResult()
                       .getResponseBody()))
                .expectNextMatches(agreementResponse -> {
                        assertNotNull(agreementResponse.getAgreementParticipant());
                        return true; 
                }) 
                .expectComplete()
                .verify();

这是我的控制器

return Mono.fromCallable(() -> { 
            myService.myMethod(object);
            return object;
        }).log().subscribeOn(Schedulers.elastic())
                .map(p -> ResponseEntity.ok(p))
                .defaultIfEmpty(ResponseEntity.notFound().build())
                .doOnError(e -> { 
                    LOGGER.error(LOG_FORMAT, e.getMessage(), e.getStackTrace());                
                }); 


Mockito is not returning exception while myService.myMethod(object) is been called.
Please suggest proper way to write test for .defaultIfEmpty and .doOnError blocks

【问题讨论】:

    标签: unit-testing mockito spring-webflux reactor


    【解决方案1】:

    在模拟你的myService.myMethod(object) 时不要抛出CriticalException.class,而是返回一个包裹在Mono 中的exception

    例如:

    Mockito.doReturn(Mono.error(Exception::new)).when(service).callableMethod();

    在下面找到示例代码sn-p

    import org.junit.Test;
    import org.mockito.Mock;
    import org.mockito.Mockito;
    import reactor.core.publisher.Mono;
    import reactor.test.StepVerifier;
    
    class Service {
    
        public Mono<String> callableMethod() {
    
            return Mono.just("1");
        }
    }
    
    class Controller {
    
        private Service service;
    
        public Controller(Service service) {
    
            this.service = service;
        }
    
        public Mono<String> endpoint() {
    
            return service.callableMethod().doOnError(throwable -> {
                System.out.println("throwable = " + throwable);
            });
        }
    }
    
    public class TestClass {
    
        @Mock
        private Service service = Mockito.mock(Service.class);
    
        @Test
        public void controllerTest() {
    
            Mockito.doReturn(Mono.error(Exception::new)).when(service).callableMethod();
    
            StepVerifier.create(new Controller(service).endpoint()).verifyError();
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2017-09-24
      • 1970-01-01
      • 1970-01-01
      • 2016-06-27
      • 1970-01-01
      • 1970-01-01
      • 2020-11-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多