英文:
Spring Boot WebFlux tests failing after upgrade to Spring Boot 2.2
问题
我有一些 Spring Boot 的集成测试,在 Spring Boot 2.1 中运行良好,但是现在我已经升级到 Spring Boot 2.2,它们失败了。使用默认的 spring-boot parent 依赖管理。一些以前工作正常的失败测试就像这个简单的例子:
...
@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,于2020年8月18日10:00:09 CEST开始启动,父级为org.springframework.context.annotation.AnnotationConfigApplicationContext@a035db9]尚未刷新,这是意外的。Reactor 上下文为 [Context0{}],名称为 [blocking]
在升级后,我有很多类似行为的测试失败。它在这一行失败:assertThat(acc.block(), hasSize(0));
可能是什么原因?
更新
根据评论的建议,我尝试将测试更改为使用 StepVerifier,但未成功:
@Test
public void verifyAccountsShouldReturnEmpty() {
StepVerifier.create(
accountService.getAccounts(new AccountId(ACC_ID)))
.expectNext(Collections.emptyList())
.verifyComplete();
}
错误的结尾刚刚变成:父级为 org.springframework.context.annotation.AnnotationConfigApplicationContext@985b9f6] 尚未刷新,这是意外的。Reactor 上下文为 [Context0{}],名称为 [stepVerifier ]))
,这似乎与使用 block 相同的问题,但现在是在 StepVerifier 中出现。
英文:
I have some Spring Boot integration tests which worked fine in Spring Boot 2.1 but now that I have upgraded to Spring Boot 2.2 they're failing. Using default spring-boot parent dependency management. Some failing tests which used to work are as simple as this example:
...
@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));
}
...
Before the upgrade, this test passes as expected but after the upgrade, it fails with the following error:
[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]
I have many tests with similar behaviour after upgrading. It fails on line assertThat(acc.block(), hasSize(0));
What might be causing this?
Update
I've tried changing the test to use StepVerifier as suggested in comments but didn't work:
@Test
public void verifyAccountsShouldReturnEmpty() {
StepVerifier.create(
accountService.getAccounts(new AccountId(ACC_ID)))
.expectNext(Collections.emptyList())
.verifyComplete();
}
The end of the error just changed to: parent: org.springframework.context.annotation.AnnotationConfigApplicationContext@985b9f6] is not yet refreshed. This is unexpected. Reactor Context is [Context0{}] and name is [stepVerifier ]))
which seems the same issue as using block but now with StepVerifier.
答案1
得分: 0
我最终设法解决了这个问题,不幸的是,我不确定根本原因是什么,但解决方法是从 @SpringBootTest 注解中移除属性,并使用其默认值。显然,在升级依赖项时发生了某些变化,导致了这个问题。
所以像这样工作:
...
@RunWith(SpringRunner.class)
// 下面的注解属性被移除了,这样测试就正常了...
@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 {
英文:
I finally manage to sort it out, unfortunately not sure of the root cause but what fixed it was removing the properties from the @SpringBootTest annotation and using it's defaults. Clearly something has changed in there with the upgrade of the dependencies which caused the issue.
So it works when like this:
...
@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 {
答案2
得分: 0
我在添加以下依赖后遇到了类似的问题:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-sleuth</artifactId>
</dependency>
我通过移除 @DirtiesContext 来解决了这个问题。
英文:
I have a similar issue after adding
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-sleuth</artifactId>
</dependency>
I solved this by removing @DirtiesContext
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论