【问题标题】:Adding spring-cloud-starter-sleuth dependency to a Spring-Boot App some of the Rest Doc test failing将 spring-cloud-starter-sleuth 依赖项添加到 Spring-Boot App 一些 Rest Doc 测试失败
【发布时间】:2019-10-17 18:50:08
【问题描述】:

我有一个 Spring-Boot 2.1.4 应用程序,其中包含用于 Flux 控制器的 n-RestDoc 测试。

环境:

  • Spring-Boot 2.1.4
  • Junit 5.4.0
  • spring-restdocs-webtestclient
  • spring-webflux

如果我将 spring-clout-starter-sleuth 依赖项添加到应用程序中,一些 doc 测试在 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 盒子上,带有通量内容的控制器的 rest doc 测试在带有单声道内容的控制器的 linux 盒子上失败。

【问题讨论】:

  • 为什么要从 Sleuth 依赖中排除 Spring 版本?
  • 非常简单 - 我有 Spring Boot 2.1.4 和 spring-framework 5.1.6 并从 sleuth 版本 2.1.0 开始,它有不同的版本,所以我有一个 spring 版本组合。
  • 信息:如果我从 sleuth 依赖项中删除所有排除项,则行为相同
  • 您为什么要更改 spring 版本?它应该来自 Boot。此外,最好有完整的堆栈跟踪或任何其他信息,因为目前我不知道如何帮助您
  • spring-cloud-starter-sleuth 来自 springframework.cloud 并且有自己的依赖关系并且不是 spring-boot 的一部分,但它没有区别,因为没有排除会发生相同的错误。

标签: spring-boot spring-webflow spring-cloud-sleuth spring-restdocs


【解决方案1】:

这已在 sleuth github 问题列表 github.com/spring-cloud/spring-cloud-sleuth/issues/1450 中作为错误提出

如果您和我一样在 junit 测试方面遇到过这种情况,那么我通过删除我项目中每个 junit 测试类中的所有注释 @DirtiesContext 来解决它。

【讨论】:

    【解决方案2】:

    如果我在 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 版本,就会出现错误。

    【讨论】:

    • 这似乎在 2.1.3-RELEASE 中也没有解决
    【解决方案3】:

    我也遇到了同样的问题。我通过将 Sleuth 降级到2.0.3.RELEASE(2.1.0 也不适用于我)解决了这个问题。我的 Spring Boot 版本是2.0.5.RELEASE.
    如果 2.0.3 仍然不适合您,请尝试从mvn repo 获得更多降级版本

    【讨论】:

      猜你喜欢
      • 2021-04-18
      • 2017-03-19
      • 2015-12-28
      • 1970-01-01
      • 2018-01-10
      • 2021-04-29
      • 1970-01-01
      • 2022-01-23
      • 1970-01-01
      相关资源
      最近更新 更多