我有下面的Spring reactor代码,它通过scheduler.slastic()在db中进行保存。然而,我看到弹性线程直到60秒(空闲时间)后才会结束/提交。所以我的集成测试失败了,除非我等待那个时间。有更好的方法吗?就像使用schedulers.immediate()进行测试和弹性部署一样。
public void method() { Mono.just(student) .flatMap(student -> populateStudentDetails(student)) .subscribeOn(Schedulers.elastic) .subscribe(studentRepository::save); }
我正在进行下面的测试
@SpringBootTest public class TestClass { @Test void testMethod() { testClass.method(); //assertForDatainDB //fails if immediately asserted //succeeds if asserted after 60s } }
正如DarrenForsyth所建议的,我必须包括StepVerifier.WithVirtualTime才能通过测试。
StepVerifier.withVirtualTime(() -> Mono.fromRunnable(() -> testClass.method()) .verifyComplete();