如何测试从服务调用的 Rest Controller 中的反应式方法
How to test a reactive method within Rest Controller called from service
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 中的客户端将使用该事件。
如何进行单元测试?
当 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测试支持的maven仓库here
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 中的客户端将使用该事件。
如何进行单元测试?
当 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测试支持的maven仓库here