我有一些 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。
谢谢。