我有一个 Spring-Boot 2.1.4 应用程序,其中包含 Flux Controller 的 n-RestDoc 测试。
环境:
- Spring-Boot 2.1.4
- Junit 5.4.0
- spring-restdocs-webtestclient
- spring-webflux
如果我将 spring-clout-starter-sleuth 依赖项添加到应用程序中,某些文档测试在 Maven 构建中会失败。 重要的是,在不同的环境下,不同的测试类会失败:
java.lang.IllegalStateException: org.springframework.boot.web.reactive.context.AnnotationConfigReactiveWebServerApplicationContext@6516dd09 has been closed already ....
如果使用 maven -Dtest=OptDocTest
运行失败的测试,则测试不会失败,即使指定了一组(不是全部)测试。
依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-sleuth</artifactId>
<version>2.1.1.RELEASE</version>
<exclusions> <!-- exclude old spring versions -->
<exclusion>
<artifactId>*</artifactId>
<groupId> org.springframework.security</groupId>
</exclusion>
<exclusion>
<artifactId>spring-aop</artifactId>
<groupId>org.springframework</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
</dependency>
所有测试看起来都很相似
@ExtendWith({ RestDocumentationExtension.class, SpringExtension.class })
@AutoConfigureRestDocs("target/generated-snippets")
@SpringBootTest(//webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
classes = { ArchimedesApplication.class })
class OptControllerDocTest {
@MockBean
private SrkConnector srkTConnector;
@Autowired
private ApplicationContext context;
private WebTestClient webTestClient;
@BeforeEach
void beforeEach(RestDocumentationContextProvider restDocumentation) {
this.webTestClient = WebTestClient.bindToApplicationContext(context)
.configureClient()
.filter(documentationConfiguration(restDocumentation))
.build();
}
@Test
void documentationTest() throws IOException {
this.webTestClient.post()
.uri("/opt")
.contentType(MediaType.APPLICATION_JSON)
.body(BodyInserters.fromObject(testRequest))
.exchange()
.expectStatus() // here the error occur
.isOk()
.expectBody() ...
}
使用正在运行的启动应用程序进行的所有 IT 测试都工作正常。
我不知道出了什么问题以及为什么 AnnotationConfigReactiveWebServerApplicationContext
被关闭。
在 Windows 盒子上,具有助焊剂内容的 Controller 的其余文档测试在具有单声道内容的 Controller 的 Linux 盒子上失败。
最佳答案
如果我回退到版本 2.1.0.RELEASE 中的 spring-cloud-starter-sleuth
并删除所有排除项,问题就解决了。
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-sleuth</artifactId>
<version>2.1.0.RELEASE</version>
</dependency>
如果我使用版本 2.1.1.RELEASE,则会发生错误。
关于spring-boot - 将 spring-cloud-starter-sleuth 依赖项添加到 Spring-Boot 应用程序中,一些 Rest Doc 测试失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55594039/