Please, refer to https://github.com/spring-cloud/spring-cloud-config/issues/2376

Can you check the case:

1) Take the demo from the Spring Cloud issue

2) Add to the test class DemoApplicationTests


@SpringBootTest(properties = {
        "spring.config.import=optional:configserver:http://localhost:8065"
            })

Than you will see that the default value for myProperty was returned instead of value from application.properties.

Is it normal? It seems that earlier we didn't have this issue.

Comment From: philwebb

@anvo1115 Can you please provide the config server application that works with the demo. I currently get "Connection refused".

Comment From: anvo1115

@philwebb , you don't need config-server application to run the test. To reproduce you just need to run DemoApplicationTests -> contextLoads test.

SpringBoot After upgrading Spring Boot from 3.1.5 to 3.2.0 properties from application.properties are missing

Comment From: anvo1115

SpringBoot After upgrading Spring Boot from 3.1.5 to 3.2.0 properties from application.properties are missing

Comment From: wilkinsona

That doesn't work for me, with or without adding the properties attribute to @SpringBootTest on DemoApplicationTests. I can only get the test to run by also removing the spring.config.import entry from application.properties. It then produces the following output:

09:39:42.179 [main] INFO org.springframework.test.context.support.AnnotationConfigContextLoaderUtils -- Could not detect default configuration classes for test class [com.example.demo.DemoApplicationTests]: DemoApplicationTests does not declare any static, non-private, non-final, nested classes annotated with @Configuration.
09:39:42.249 [main] INFO org.springframework.boot.test.context.SpringBootTestContextBootstrapper -- Found @SpringBootConfiguration com.example.demo.DemoApplication for test class com.example.demo.DemoApplicationTests

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::       (v3.1.9-SNAPSHOT)

2024-03-19T09:39:42.776Z  INFO 32213 --- [           main] com.example.demo.DemoApplicationTests    : Starting DemoApplicationTests using Java 17.0.1 with PID 32213 (started by awilkinson in /Users/awilkinson/Downloads/demo)
2024-03-19T09:39:42.777Z  INFO 32213 --- [           main] com.example.demo.DemoApplicationTests    : No active profile set, falling back to 1 default profile: "default"
2024-03-19T09:39:43.131Z  INFO 32213 --- [           main] o.s.cloud.context.scope.GenericScope     : BeanFactory id=d9e6cb92-a6b3-3fe6-81fb-6259ea55e64e
2024-03-19T09:39:43.334Z  INFO 32213 --- [           main] com.example.demo.DemoApplicationTests    : Started DemoApplicationTests in 0.959 seconds (process running for 1.584)
Java HotSpot(TM) 64-Bit Server VM warning: Sharing is only supported for boot loader classes because bootstrap classpath has been appended

Note that lack of **** myProperty … output.

The sample's also using 3.1.9-SNAPSHOT but this issue's title says that the problem only occurs with 3.2.

Can you please provide a minimal sample that reproduces the problem without requiring any modifications?

Comment From: anvo1115

I will be able to update Demo after the release of https://github.com/spring-cloud/spring-cloud-config/issues/2376#issuecomment-1996131515

Comment From: spring-projects-issues

If you would like us to look at this issue, please provide the requested information. If the information is not provided within the next 7 days this issue will be closed.

Comment From: anvo1115

Please, check the attached demo project. I upgraded spring-cloud and spring-boot. demo.zip

Comment From: philwebb

Thanks for the sample @anvo1115.

From a Spring Boot perspective I think this is working as designed. Any spring.config.import value is evaluated with the properties that have been loaded up to that point.

When you have:

@SpringBootTest(properties = {"spring.config.import=optional:configserver:http://localhost:8065"})

Spring Boot will attempt to import from optional:configserver:http://localhost:8065 before it's attempted to load any other properties. This means that my-property hasn't been set because none of the application.properties files have even started to be loaded.

If you remove properties from the @SpringBootTest annotation then things do work because the spring.config.import is initiated from the value in application.properties and by that time my-property is available.

Likewise, if you did the following:

@SpringBootTest(properties = {"spring.config.import=optional:configserver:http://localhost:8065", "my-property=foo"})

You'd also see my-property being resolved.

I'm not sure why things changed when you upgraded dependencies. I don't think we've touched the area of code in Spring Boot that handles imports so I suspect it's something in Spring Cloud.

As a fix, I would suggest updating your application.properties to something like this:

my-property=foo
myserver=http://therealserver.example.com:8065
spring.config.import=optional:configserver:${myserver}

Then in your test do:

@SpringBootTest(properties = { "myserver=http://localhost:8065" })

That way the spring.config.import is initiated when application.properties is loaded which is late enough for my-property to be available.