升级到Spring Boot 2.2后,Springboot WebFlux测试失败

问题描述

我有一些Springboot集成测试,在Springboot 2.1中可以正常工作,但是现在我已升级到Springboot 2.2,但是它们失败了。使用认的spring-boot父依赖管理。曾经起作用的一些失败测试就像这个示例一样简单:

...
@RunWith(springrunner.class)
@SpringBoottest(
 webEnvironment = SpringBoottest.WebEnvironment.RANDOM_PORT,properties = {"spring.sleuth.enabled=false"})
@Import({Accountapiclient.class,...})
@DirtiesContext
@AutoConfigureStubRunner(stubsMode = StubRunnerProperties.StubsMode.CLAsspATH,ids = {"my.org.com:account-service:+:stubs:9021"},consumerName = "AccountServiceV2",stubsPerConsumer = true)
public class AccountsClientTest {

@Autowired
AccountService accountService;

@Test
public void verifyAccountsShouldReturnEmpty() {
    Mono<List<Accounts>> acc = accountService.getAccounts(new AccountId(ACC_ID));
    assertthat(acc.block(),hasSize(0));
 }
 ...

升级前,此测试按预期通过,但升级后失败,并显示以下错误

[ERROR] verifyAccountsShouldReturnEmpty  Time elapsed: 0.004 s  <<< ERROR!
reactor.core.Exceptions$ReactiveException: java.lang.AssertionError: Spring Context [org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@7d605944,started on Tue Aug 18 10:00:09 CEST 2020,parent: org.springframework.context.annotation.AnnotationConfigApplicationContext@a035db9] is not yet refreshed. This is unexpected. Reactor Context is [Context0{}] and name is [blocking]

升级后,我有许多行为相似的测试。它在第assertthat(acc.block(),hasSize(0));行上失败

可能是什么原因造成的?

更新

我尝试按照注释中的建议将测试更改为使用StepVerifier,但无法正常工作:

@Test
public void verifyAccountsShouldReturnEmpty() {
  StepVerifier.create(
      accountService.getAccounts(new AccountId(ACC_ID)))
      .expectNext(Collections.emptyList())
      .verifyComplete();
}

错误的结尾刚刚更改为:parent: org.springframework.context.annotation.AnnotationConfigApplicationContext@985b9f6] is not yet refreshed. This is unexpected. Reactor Context is [Context0{}] and name is [stepVerifier ])),这似乎与使用block的问题相同,但现在使用了StepVerifier。

谢谢。

解决方法

我终于设法解决了这个问题,不幸的是不确定根本原因,但是解决了它的问题是从@SpringBootTest批注中删除了属性并使用了默认值。显然,随着引起问题的依赖关系的升级,那里发生了一些变化。

因此它可以在这样的情况下工作:

...
@RunWith(SpringRunner.class)

// below annotation properties were removed which fixed the tests...
@SpringBootTest

@Import({AccountApiClient.class,...})
@DirtiesContext
@AutoConfigureStubRunner(stubsMode = StubRunnerProperties.StubsMode.CLASSPATH,ids = {"my.org.com:account-service:+:stubs:9021"},consumerName = "AccountServiceV2",stubsPerConsumer = true)
public class AccountsClientTest {
,

添加后我也遇到类似的问题

    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-sleuth</artifactId>
    </dependency>

我通过删除@DirtiesContext解决了这个问题