Spring Boot 集成测试抛出错误“java.lang.IllegalStateException: Timeout on blocking read for 5000 MILLISECONDS

Posted

技术标签:

【中文标题】Spring Boot 集成测试抛出错误“java.lang.IllegalStateException: Timeout on blocking read for 5000 MILLISECONDS”【英文标题】:Spring Boot Integration test throws error "java.lang.IllegalStateException: Timeout on blocking read for 5000 MILLISECONDS" 【发布时间】:2018-01-18 20:30:15 【问题描述】:

这个问题发生在我通过 Spring boot 2.0.0.M3 使用 Spring webflux 的项目上。下面是项目的依赖,

dependencies 
        compile 'org.springframework.boot:spring-boot-starter-actuator',
                'org.springframework.cloud:spring-cloud-starter-config',
                'org.springframework.cloud:spring-cloud-sleuth-stream',
                'org.springframework.cloud:spring-cloud-starter-sleuth',
                'org.springframework.cloud:spring-cloud-starter-stream-rabbit',
                'org.springframework.boot:spring-boot-starter-data-mongodb-reactive',
                'org.springframework.boot:spring-boot-starter-data-redis-reactive',
                'org.springframework.boot:spring-boot-starter-integration',
                "org.springframework.integration:spring-integration-amqp",
                "org.springframework.integration:spring-integration-mongodb",
                'org.springframework.retry:spring-retry',
                'org.springframework.boot:spring-boot-starter-webflux',
                "org.springframework.boot:spring-boot-devtools",
                'com.fasterxml.jackson.datatype:jackson-datatype-joda',
                'joda-time:joda-time:2.9.9',
                'org.javamoney:moneta:1.0',
                'com.squareup.okhttp3:okhttp:3.8.1',
                "net.logstash.logback:logstash-logback-encoder:4.11",
                'org.apache.commons:commons-lang3:3.5'
        compileOnly 'org.projectlombok:lombok:1.16.18'
        testCompile 'org.springframework.boot:spring-boot-starter-test',
                'io.projectreactor:reactor-test',
                'org.apache.qpid:qpid-broker:6.1.2',
                'de.flapdoodle.embed:de.flapdoodle.embed.mongo'

        integTestCompile sourceSets.main.output
        integTestCompile configurations.testCompile
        integTestCompile sourceSets.test.output
        integTestRuntime configurations.testRuntime
    

我有一个 REST API,代码如下:

 @RestController
    @RequestMapping(value="/internal/note")
    @Slf4j
    public class NoteController 

        @Autowired
        NoteRepository noteRepository;

        @GetMapping("/request/id")
        public Mono<Note> getNoteByRequestid(@PathVariable("id") String requestid) 
            logger.debug("Requesting note by request id ''.", requestid);
            return noteRepository.findByRequestid(requestid).doOnSuccess(note ->
                    logger.debug("Found note ''.", note))
                    .doOnSuccess(response -> logger.debug("Successfully found note by request id ''.", requestid));
        
    

代码运行良好。使用 Postman 调用 API 时,返回正确的结果。

我有如下集成测试代码:

final String data = "xxxxx";//JSON Data
    final Note newNote = noteRepository.save(objectMapper.readValue(data, Note.class)).block(Duration.ofSeconds(3));
            final String requestid = "5982C7366FF1C55D20CF2B70";
            this.webClient.get().uri("/internal/note/request/id", requestid).accept(MediaType.APPLICATION_JSON_UTF8)
                    .exchange().expectStatus().isOk();

但测试失败并抛出如下错误消息:

 java.lang.IllegalStateException: Timeout on blocking read for 5000 MILLISECONDS

        at reactor.core.publisher.BlockingSingleSubscriber.blockingGet(BlockingSingleSubscriber.java:109)
        at reactor.core.publisher.Mono.block(Mono.java:1304)
        at org.springframework.test.web.reactive.server.DefaultWebTestClient$DefaultRequestBodyUriSpec.toResponseSpec(DefaultWebTestClient.java:291)
        at org.springframework.test.web.reactive.server.DefaultWebTestClient$DefaultRequestBodyUriSpec.exchange(DefaultWebTestClient.java:269)
        at cn.demo.handler.NoteHandlerTest.testGetNoteByRequestidWithPriceField(NoteHandlerTest.java:62)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
        at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
        at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
        at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
        at org.springframework.test.context.junit4.statements.RunBeforeTestExecutionCallbacks.evaluate(RunBeforeTestExecutionCallbacks.java:73)
        at org.springframework.test.context.junit4.statements.RunAfterTestExecutionCallbacks.evaluate(RunAfterTestExecutionCallbacks.java:83)
        at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
        at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
        at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
        at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
        at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
        at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:251)
        at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:97)
        at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
        at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
        at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
        at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
        at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
        at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
        at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
        at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
        at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:190)
        at org.junit.runners.Suite.runChild(Suite.java:128)
        at org.junit.runners.Suite.runChild(Suite.java:27)
        at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
        at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
        at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
        at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
        at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
        at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
        at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
        at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
        at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:51)
        at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
        at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)

关于如何解决问题的任何建议?

【问题讨论】:

我有同样的问题,我通过覆盖超时来解决它***.com/a/49496309/4215474 我超过了超时,它对我有用检查***.com/a/49496309/4215474 您找到解决错误的方法了吗?我遇到了类似的问题。 【参考方案1】:

这可能是框架中的一个错误,但考虑到您在类路径中获得的东西的数量,您应该尝试创建一个最小的示例项目来重现它并针对 Spring 框架打开一个问题 (https://jira.spring.io) .

【讨论】:

以上是关于Spring Boot 集成测试抛出错误“java.lang.IllegalStateException: Timeout on blocking read for 5000 MILLISECONDS的主要内容,如果未能解决你的问题,请参考以下文章

Spring Boot Main 类 Junit 测试抛出错误

Spring Boot 集成测试无法访问 application.properties 文件

Java - Spring boot - 集成测试 - TestEntityManager 没有被注入

在 spring-boot 集成测试中使用 H2 数据库的 JDBC 连接错误

Spring Boot 集成测试因 java.awt.HeadlessException 而失败

构建 maven spring boot 项目抛出错误