Top Spring Boot Context with Remote Configuration from JUnit Test

I am writing integration tests in a microservice environment. I want to fully initialize my Spring application context once when I run my JUnit test class. I can do this fairly easily under normal circumstances, but in this case I need to load the remote configuration from the Git repository (I use spring -cloud-config). Under normal startup conditions, this Spring Boot application successfully loads the configuration from the Git repository. I am just trying to reproduce the same context initialization in the testing phase. This is how I wrote a 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} 

Log 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 you can see above, starting is not possible because the driver class is not defined for my protection level. Since the corresponding configuration is in Git, this error indicates that the remote configuration is not retrieved.

I appreciate any help anyone can offer. I will update this question with additional information, if necessary.

+5
source share

Source: https://habr.com/ru/post/1263945/


All Articles