Search code examples
springgradlespring-bootspring-cloudspring-cloud-config

Spring Boot with spring-cloud: gradle build fails


./gradlew build fails with the error given at the bottom while running :test task. The code just checks if the context is loaded properly.

@RunWith(SpringRunner.class)
@SpringBootTest
@ContextConfiguration
public class RegistryApplicationTests {

    @Test
    public void contextLoads() {
    }
}

The bootstrap.yml file is given below(pretty standard), I'm not sure why it is trying to load the property file from the cloud-config service, how do I work around it??

spring:
 application:
   name: registry
 profiles:
   active: default
 cloud:
   config:
     uri: http://localhost:8888
     fail-fast: true

eureka:
  instance:
    prefer-ip-address: true
  client:
    registerWithEureka: false
    fetchRegistry: false
    server:
      waitTimeInMsWhenSyncEmpty: 0

Stack Trace

Caused by: java.lang.IllegalStateException: Could not locate PropertySource and the fail fast property is set, failing
    at org.springframework.cloud.config.client.ConfigServicePropertySourceLocator.locate(ConfigServicePropertySourceLocator.java:130)
    at org.springframework.cloud.bootstrap.config.PropertySourceBootstrapConfiguration.initialize(PropertySourceBootstrapConfiguration.java:89)
    at 
    ....
    ....
Caused by: org.springframework.web.client.ResourceAccessException: I/O error on GET request for "http://localhost:8888/registry/default": Connection refused: connect; nested exception is java.net.ConnectException: Connection refused: connect
    at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:666)
    at 
    ....
    ....
Caused by: java.net.ConnectException: Connection refused: connect
    at java.net.DualStackPlainSocketImpl.connect0(Native Method)
    at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79)
    at 

UPDATE As suggested by @dzatorsky tried adding @Profile("test") and @ActiveProfiles("test"),DID NOT WORK.

Tried manually adding the property file for the test using @TestPropertySource(locations = "file:src/test/resources/application-test.yml") DID NOT WORK

Finally overrode using @TestPropertySource(properties = {"spring.cloud.config.fail-fast=false"}) which worked, but it looks like a very ugly work around

The Inference is bootstrap.yml in src/main/resources overrides the properties specified anywhere else, tried renaming application-test.yml to bootstrap.yml in src/test/resources WORKED.

Is this the cleaner way getting this done?


Solution

  • Error on GET request for "http://localhost:8888/registry/default": Connection refused: connect; nested exception is java.net.ConnectException: Connection refused

    Seems that your Spring Cloud Config server is down.

    UPDATE: If you want to run your tests without running Config Server (which is a right thing to do in the most cases) then I would suggest doing the following:

    Add application-test.yml with the following content:

    cloud:
        config:
            fail-fast: false
    

    Annotate your test class with:

    @Profile("test")
    

    In this case, whenever you run your tests they use a default parameters defined in application.yml plus those that you override in application.test.yml.