升级到 spring boot 2.2 后,Springboot WebFlux 测试失败
Springboot WebFlux tests failing after upgrade to spring boot 2.2
我有一些 Springboot 集成测试在 Springboot 2.1 中运行良好,但现在我已经升级到 Springboot 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, 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。
谢谢。
我终于设法解决了这个问题,不幸的是不确定根本原因,但修复它的方法是从 @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
解决了这个问题
我有一些 Springboot 集成测试在 Springboot 2.1 中运行良好,但现在我已经升级到 Springboot 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, 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。
谢谢。
我终于设法解决了这个问题,不幸的是不确定根本原因,但修复它的方法是从 @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
解决了这个问题