【问题标题】:How to test a reactive method within Rest Controller called from service如何测试从服务调用的 Rest Controller 中的反应方法
【发布时间】:2018-09-14 08:14:42
【问题描述】:
The following is my rest controller code 

    @RequestMapping(produces = MediaType.TEXT_EVENT_STREAM_VALUE, value = "/myMethod")
        public Flux<Integer> myMethod() {       
            return service.myServiceMethod();
        }


My service method is   

      public Flux<Integer> myServiceMethod() {
                return Flux.fromStream(Stream.generate(() -> no++).map(s -> Integer.valueOf(s))).delayElements(Duration.ofSeconds(5));
            }

I am trying to write a test as below but it is not working

@Autowired
    NumberEventService serv; 

@Test
    public void test() {
      StepVerifier.withVirtualTime(() -> 
            Flux.just(serv.generateNumberEvent()).delayElements(Duration.ofSeconds(5)))
            .expectSubscription() //t == 0  
            .expectNoEvent(Duration.ofSeconds(1))   
            .expectNextCount(1) 
            .expectNoEvent(Duration.ofSeconds(1))
            .expectNextCount(1) 
            .expectNoEvent(Duration.ofSeconds(1))
            .expectNextCount(1) 
            .expectComplete()   
            .verify();
    }

我得到一个错误

java.lang.BootstrapMethodError: java.lang.NoClassDefFoundError: reactor/core/scheduler/TimedScheduler
    at reactor.test.StepVerifier.withVirtualTime(StepVerifier.java:167)
    at reactor.test.StepVerifier.withVirtualTime(StepVerifier.java:140)

我在端口 8080 中启动了应用程序并运行了测试。我在做什么错误?

我将在 8080 中启动此方法,并且端口 8082 中的客户端将使用该事件。

如何进行单元测试?

【问题讨论】:

  • 一个 NCDFE 表明存在依赖关系问题。你能发布你的pom.xmlbuild.gradle 吗?

标签: spring unit-testing spring-webflux reactor


【解决方案1】:

reactor-core 版本与 spring-starter-reactor 带来的和 reactor-test 插件不匹配时,您将遇到 NoClassDefFoundError

您必须查看您的 spring-boot-starter-webflux 依赖项,并搜索 reactor-core 模块的版本。

然后您将能够添加正确版本的reactor-test 模块。

在我的示例中,reactor-core 模块的当前版本是3.1.8.RELEASE,所以我必须添加相同版本的reactor-test 模块。

可以查看reactor测试支持here的maven仓库

【讨论】:

    猜你喜欢
    • 2017-02-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-24
    相关资源
    最近更新 更多