Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Start Spring Boot context with remote config from JUnit Test

I am writing integration tests in a microservice environment. I want to fully initialize my Spring Boot application context once when running my JUnit test class. I can do this easily enough under normal circumstances, but in this case I need to load remote configuration from a Git repository (I am using spring-cloud-config). Under normal startup conditions, this Spring Boot application does successfully load the configuration from the Git repo. I am just trying to replicate the same context initialization during the test phase. Here is how I have written the test class:

@RunWith(SpringRunner.class)
@SpringBootTest(properties={"ENV=local","spring.cloud.config.enabled=true","spring.cloud.config.uri=http://config.server.com/config","ENCRYPT_KEY=secret"})
@ContextConfiguration(classes ={MyAppConfig.class,MyDBConfig.class})
@PropertySource({"classpath:spring-cloud-config.properties"})
public class MyIntegrationTest {

    @Test
    public void testFindUsersForCorp() {
        System.out.println("Test is running ");
    }
}

spring-cloud-config.properties:

spring.profiles.active=${ENV}
spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration
spring.cloud.consul.host=${CONSUL.HOST:localhost}
spring.cloud.consul.port=${CONSUL.PORT:8500}
spring.cloud.consul.discovery.healthCheckPath=/custom/health
spring.cloud.consul.discovery.healthCheckInterval=30s
spring.cloud.config.uri=${CONFIG.SERVER.URI:http://config.server:80/config}
spring.cloud.config.username=user
spring.cloud.config.password=${PASSWORD}

Logging output during Spring context initialization:

main WARN: Could not locate PropertySource: I/O error on GET request for "http://localhost:8888/userdata-service/default": Connection refused: connect; nested exception is java.net.ConnectException: Connection refused: connect
main WARN: Cannot enhance @Configuration bean definition 'refreshScope' since its singleton instance has been created too early. The typical cause is a non-static @Bean method with a BeanDefinitionRegistryPostProcessor return type: Consider declaring such methods as 'static'.
main ERROR: 

***************************
APPLICATION FAILED TO START
***************************

Description:

Cannot determine embedded database driver class for database type NONE
[OMITTED]

As seen above, the startup fails because no driver class for my persistance layer is defined. Since the relevant config is in Git, this error indicates that the remote configuration is not being fetched.

I appreciate any help anyone can offer. I will update this question with more information if needed.

like image 874
diogenes_der_zyniker Avatar asked Feb 07 '17 23:02

diogenes_der_zyniker


1 Answers

Spring Cloud Config client should be initialized in bootstrap phase, so you should move corresponding properties from spring-cloud-config.properties to bootstrap.properties See https://cloud.spring.io/spring-cloud-config/multi/multi__spring_cloud_config_client.html#config-first-bootstrap

like image 60
Yurii J Avatar answered Oct 15 '22 09:10

Yurii J