0

我有一些 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 ]))这似乎与使用块相同的问题,但现在使用 StepVerifier。

谢谢。

4

2 回答 2

0

我终于设法解决它,不幸的是不确定根本原因,但修复它的原因是从 @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 {
于 2020-08-19T13:55:31.047 回答
0

添加后我有类似的问题

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

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

于 2020-08-27T15:42:30.300 回答