升级到SpringBoot2.2后SpringBootWebFlux测试失败

zwghvu4y  于 2021-07-12  发布在  Java
关注(0)|答案(2)|浏览(459)

我有一些springboot集成测试,在springboot2.1中运行良好,但是现在我已经升级到springboot2.2,它们失败了。使用默认的spring启动父依赖关系管理。一些过去有效的失败测试如以下示例所示:

...
@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。
谢谢。

t40tm48m

t40tm48m1#

我最终设法解决了这个问题,不幸的是,我不确定根本原因,但修复它的方法是从@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 {
vawmfj5a

vawmfj5a2#

我有一个类似的问题后添加

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

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

相关问题