springcloud 2020.0.2升级生成测试错误

mrwjdhj3  于 2021-07-15  发布在  Java
关注(0)|答案(2)|浏览(746)

我正在尝试从springcloud bom升级一个项目 2020.0.12020.0.2 当我更改bom并重新构建时,我在junit测试中得到一个错误,即新参数 spring.config.import 未为configserver设置。
此项目不是configserver,也不使用configserver(已注解的config客户端)
这是测试中报告的错误 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

这是我的身材。grad尔

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=" })

ogsagwnx

ogsagwnx1#

在升级到springcloud2020.0.2之后,我注意到了同样的问题
添加 spring.cloud.config.enabled=false 在测试中解决了这个问题。
例如。:

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

y0u0uwnf2#

我也遇到了同样的问题,通过添加bootstrap lib和config lib解决了这个问题,如下所示,

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

相关问题