I have a bare Spring Boot application
@SpringBootApplication
public class ClientApplication {
public static void main(String[] args) {
SpringApplication.run(ClientApplication.class, args);
}
}
which connects to a Spring Cloud Config Server with the following application.yml
spring:
application:
name: client
config:
import: configserver:http://localhost:8888
The application works fine when the config server is running and fails as expected when the server is not running.
I now want to write an integration test with @SpringBootTest
for the application that does not depend on a running config server, and does not even try to connect to it.
With the config server down, the bare test
@SpringBootTest
class ClientApplicationTests {
@Test
void contextLoads() {
}
}
fails with java.net.ConnectException: Connection refused
, which is expected.
When I try to disable the config client with
@SpringBootTest(properties = "spring.cloud.config.enabled=false")
the test does not try to connect to the server but fails with
Failed to load ApplicationContext
java.lang.IllegalStateException: Failed to load ApplicationContext
at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:132)
...
Caused by: java.lang.IllegalStateException: Unable to load config data from 'configserver:http://localhost:8888'
at org.springframework.boot.context.config.StandardConfigDataLocationResolver.getReferences(StandardConfigDataLocationResolver.java:141)
...
Caused by: java.lang.IllegalStateException: File extension is not known to any PropertySourceLoader. If the location is meant to reference a directory, it must end in '/' or File.separator
at org.springframework.boot.context.config.StandardConfigDataLocationResolver.getReferencesForFile(StandardConfigDataLocationResolver.java:229)
The problem is that the property spring.config.import
that has been newly introduced with Spring Boot 2.4 works differently than all other properties. It seems to me that only values can be added to it but never removed.
Is there a way to override spring.config.import
in a SpringBootTest
? Is there way to suppress only connections to config servers?