【问题标题】:SpringCloud 2020.0.2 upgrade generates testing errorSpringCloud 2020.0.2 升级产生测试错误
【发布时间】:2021-06-23 01:24:18
【问题描述】:

我正在尝试将项目从 SpringCloud BOM 2020.0.1 升级到 2020.0.2

当我更改 BOM 并重新构建时,我在 JUnit 测试中收到错误,说新参数 spring.config.import 没有为 configserver 设置。

这个项目不是 ConfigServer,也没有使用 ConfigServer(注释配置客户端)

这是测试报告的错误contextLoads()

java.lang.IllegalStateException: Failed to load ApplicationContext
    at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:132)
    at org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:124)
    at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:118)
    .. Many more
Caused by: org.springframework.cloud.config.client.ConfigServerConfigDataMissingEnvironmentPostProcessor$ImportException: No spring.config.import set
    at org.springframework.cloud.config.client.ConfigServerConfigDataMissingEnvironmentPostProcessor.postProcessEnvironment(ConfigServerConfigDataMissingEnvironmentPostProcessor.java:60)
    at org.springframework.boot.env.EnvironmentPostProcessorApplicationListener.onApplicationEnvironmentPreparedEvent(EnvironmentPostProcessorApplicationListener.java:100)
    at org.springframework.boot.env.EnvironmentPostProcessorApplicationListener.onApplicationEvent(EnvironmentPostProcessorApplicationListener.java:86)
    ... Many more

这是我的 build.gradle

plugins {
    id 'org.springframework.boot' version '2.4.2'
    id 'io.spring.dependency-management' version '1.0.11.RELEASE'
    id 'java'
}

group = 'com.example.microservices.composite.product'
version = '1.0.0-SNAPSHOT'
sourceCompatibility = '1.8'

repositories {
    mavenCentral()
    maven {
        url 'https://repo.spring.io/milestone'
    }
}

ext {
   // resilience4jVersion = "1.7.0"
   resilience4jVersion = "1.6.1"

}

dependencies {
    // Local projects dependencies
    implementation project(':api')
    implementation project(':util') 

    // Implementations dependencies
    // Standard (actuator - for monitoring and Health)  
    implementation 'org.springframework.boot:spring-boot-starter-actuator'
    // WebFlux (asynchronous Web)
    implementation 'org.springframework.boot:spring-boot-starter-webflux'

    // SpringFox dependencies
    implementation "io.springfox:springfox-boot-starter:3+"
    implementation('io.springfox:springfox-spring-webflux:3+')

    // Implementation: Spring cloud
    implementation('org.springframework.cloud:spring-cloud-starter-config')
    implementation('org.springframework.cloud:spring-cloud-starter-stream-rabbit')
    implementation('org.springframework.cloud:spring-cloud-starter-stream-kafka')

    // Security
    implementation('org.springframework.boot:spring-boot-starter-security')
    implementation('org.springframework.security:spring-security-oauth2-resource-server')
    implementation('org.springframework.security:spring-security-oauth2-jose')

    // CircuitBreaker with Resilience4J
    implementation("io.github.resilience4j:resilience4j-spring-boot2:${resilience4jVersion}")
    implementation("io.github.resilience4j:resilience4j-reactor:${resilience4jVersion}")
  
    // Implementation: Tracing
    implementation('org.springframework.cloud:spring-cloud-starter-sleuth') 

    // Implementation: Performance metrics
    implementation("io.micrometer:micrometer-registry-prometheus")
    
    // TEST dependencies
    testImplementation('org.springframework.boot:spring-boot-starter-test') {
        exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
    }
    testImplementation 'io.projectreactor:reactor-test'
    testImplementation('org.springframework.cloud:spring-cloud-stream-test-support')

}


dependencyManagement {
    imports {
        // mavenBom 'org.springframework.cloud:spring-cloud-dependencies:2020.0.1'
        mavenBom "org.springframework.cloud:spring-cloud-dependencies:2020.0.2"
    }
}

test {
    useJUnitPlatform()
}

我在测试类中的 contextLoads() 方法很简单

// Test: Application
@AutoConfigureWebTestClient
@SpringBootTest(
    webEnvironment = WebEnvironment.RANDOM_PORT, 
    classes = { 
        ProductCompositeServiceApplication.class,
        TestSecurityConfig.class }, 
    properties = { 
        "spring.main.allow-bean-definition-overriding=true" })

    @Test
    public void contextLoads() {
    }
}

注意:我也尝试在类中将 `spring.config.import' 属性定义为空或无,没有变化

@SpringBootTest(
    webEnvironment = WebEnvironment.RANDOM_PORT, 
    classes = { 
        ProductCompositeServiceApplication.class,
        TestSecurityConfig.class }, 
    properties = { 
        "spring.main.allow-bean-definition-overriding=true",
        "spring.config.import=" })

【问题讨论】:

  • 是否没有异常分析器在日志中有建议?
  • 我不知道。我怎样才能为 Spring Boot 项目获得这个?
  • 它会在日志中
  • 我已经包含了日志(测试报告)。请记住,失败是在 gradle 构建期间,在测试阶段,没有运行程序。
  • 仅供参考,春季云版本 2020.0.3 仍将继续。

标签: spring spring-boot spring-cloud spring-test


【解决方案1】:

我解决了这个问题。添加依赖

<dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-bootstrap</artifactId>
</dependency>

【讨论】:

  • 这解决了我的问题
【解决方案2】:

您可以通过将这些行添加到test/resources 文件夹中的application.yml 文件来禁用导入检查:

spring:
  cloud:
    config:
       import-check:
          enabled: false

【讨论】:

    【解决方案3】:

    我遇到了同样的问题,并通过添加引导库和配置库来解决,如下所示,

    implementation 'org.springframework.cloud:spring-cloud-starter-config'
    implementation 'org.springframework.cloud:spring-cloud-starter-bootstrap'
    

    【讨论】:

    • 完全为我工作!谢谢
    • 这绝对解决了问题。谢谢
    【解决方案4】:

    升级到 SpringCloud 2020.0.2 后我注意到同样的问题

    在测试中添加spring.cloud.config.enabled=false 解决了这个问题。

    例如:

    @SpringBootTest(
      webEnvironment = RANDOM_PORT, 
      properties = {"spring.cloud.config.enabled=false"}
    )
    

    【讨论】:

    • 非常感谢你,Magnus(恭喜这本书!)。我试过了,这解决了测试阶段的问题,但现在主程序失败了。应用程序启动失败 ****************************** 没有定义 spring.config.import 属性操作:添加 spring.config.import =configserver:您的配置的属性。如果不需要配置,请添加 spring.config.import=optional:configserver: 。要禁用此检查,请设置 spring.cloud.config.enabled=false 或 spring.cloud.config.import-check.enabled=false。
    • 看来springfox项目添加了spring-cloud-starter-config的依赖,配置客户端现在有这个行为。我已在 application.yml 中为所有未使用配置服务器的章节添加了该属性。
    猜你喜欢
    • 1970-01-01
    • 2018-09-10
    • 2020-01-31
    • 2014-06-12
    • 1970-01-01
    • 1970-01-01
    • 2018-12-04
    • 1970-01-01
    • 2015-11-25
    相关资源
    最近更新 更多