【发布时间】: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