简体   繁体   English

如何测试返回 Mono 的控制器<ResponseEntity<Void> &gt;?

[英]How to test Controller that returns Mono<ResponseEntity<Void>>?

I'm pretty new to webflux and I am struggling to understand how to test this Controller function.我对 webflux 很陌生,我很难理解如何测试这个 Controller 功能。

    public Mono<ResponseEntity<Void>> functionName(final Request request) {
    RequestDto Dto = RequestMapper.
            INSTANCE.toDto(request);
    service.functionName(Dto);
    return Mono.just(new ResponseEntity<Void>(HttpStatus.OK));
}

You could use WebTestClient that provides fluent API for verifying responses.您可以使用WebTestClient提供流畅的 API 来验证响应。 Here is a short example这是一个简短的例子

@WebFluxTest
class ControllerTest {

    @Autowired
    private WebTestClient client;

    @BeforeEach
    void setUp(ApplicationContext context) {
        client = WebTestClient.bindToApplicationContext(context).build();
    }

    @Test
    void test() {
        client.get()
                .uri("/test")
                .exchange()
                .expectStatus().isOk()
                .expectBody().isEmpty();
    }
}

In addition, pay attention to endpoint implementation.另外,注意端点的实现。 In reactive you need to build the flow and Webflux will subscribe to it for every request.在响应式中,您需要构建流程,Webflux 将为每个请求订阅它。 In case, service.functionName is blocking (non-reactive), consider running it on a separate Scheduler using .subscribeOn(Schedulers.boundedElastic()) .如果service.functionName阻塞(非响应式),请考虑使用.subscribeOn(Schedulers.boundedElastic())在单独的Scheduler上运行它。 For details, check How Do I Wrap a Synchronous, Blocking Call?有关详细信息,请查看如何打包同步阻塞呼叫? . .

If the Callable resolves to null , the resulting Mono completes empty and we could return 404 applying switchIfEmpty operator.如果Callable解析为null ,则生成的Mono完成为空,我们可以应用switchIfEmpty运算符返回404

@GetMapping(path = "/test")
public Mono<ResponseEntity<Void>> functionName(Request request) {
    return Mono.fromCallable(() -> {
                RequestDto Dto = RequestMapper.INSTANCE.toDto(request);
                return service.functionName(Dto);
            }).subscribeOn(Schedulers.boundedElastic())
            .map(res -> new ResponseEntity<>(HttpStatus.OK))
            .switchIfEmpty(Mono.just(new ResponseEntity<>(HttpStatus.NOT_FOUND)));
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何模拟返回`Mono的方法<Void> ` - How to mock a method that returns `Mono<Void>` 如何将返回 `void` 的同步方法包装到 Mono<void> ?</void> - How to wrap synchronous method that returns `void` into Mono<Void>? 如果controller方法返回ResponseEntity,如何使用spring重定向 - How to use spring redirect if controller method returns ResponseEntity 当控制器返回ResponseEntity时,如何在Filter中设置响应状态代码? - How to set response status code in Filter when controller returns ResponseEntity? 如何对spring mvc控制器发送的ResponseBody或ResponseEntity进行单元测试? - How to unit test a ResponseBody or ResponseEntity sent by a spring mvc Controller? 如何发送具有Flux / Mono属性的类的ResponseEntity的Mono - How to send Mono of ResponseEntity of a class which has Flux/Mono attribute 如何测试 function 返回 Mono<void> 其中有另一个 Mono<t> 在使用 Reactor 的 StepVerifier</t></void> - How to test function returning Mono<Void> which has another Mono<T> within using Reactor's StepVerifier Mono怎么样<void>和 Mono.empty() 不同</void> - How are Mono<Void> and Mono.empty() different 单元测试如何使用返回无效的方法来“测试合同”? - How can a unit test “test the contract” on a method that returns void? WebFlux - 如何检查 Mono <responseentity<flux<myitem> &gt;&gt; 为空返回 404 </responseentity<flux<myitem> - WebFlux - how to check if Mono<ResponseEntity<Flux<MyItem>>> is empty to return 404
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM