SpringBootTest doesn't import local properties

189 Views Asked by At

I am running full app test and would like to read in all of the properties for my app from src/test/resources but when I add the @TestPropertySource and locations, the app still fails to load.

In production, my config is hosted on an external server and I am able to successfully load the config for local testing with this in the boostrap.yml: spring.cloud.config.uri: {pathToDevelopmentConfigServer} However, when I remove that in favor of trying to read config locally, I can't get load the app.

My setup: Test class:

@SpringBootTest
@EnableConfigurationProperties
@TestPropertySource(locations = {"classpath:MyApp.yaml", "classpath:MyApp-test.yaml"})
@ActiveProfiles(profiles = {"default", "test"})
@ContextConfiguration(classes = {MyAppProperties.class})
public class MyAppTestClass {
  
  @Test
  public void myTest(){
    assertTrue(true); //dummy test just to have one for now
  }
}

Configuration class:

@Configuration
@ConfigurationProperties(prefix = "test-config")
@Validated
public class MyAppProperties {

  @NotNull
  private String myPropertyValue;
  
  @NotNull
  private Map<String, String> myPropertyMap;

  // Getters & Setters
}

Config file (MyApp-test.yaml):

test-config:
  myPropertyValue: "testValue"
  myPropertyMap:
    mapKey: "myMapTestValue"

However, when I try to run this, I get this error:

Description:

Binding to target org.springframework.boot.context.properties.bind.BindException: Failed to bind properties under '-test' to com.c.taylor.properties.MyAppProperties$$EnhancerBySpringCGLIB$$fd769f1a failed:

    Property: test-config.myPropertyValue
    Value: null
    Reason: must not be null

    Property: test-config.myPropertyMap
    Value: null
    Reason: must not be null


Action:

Update your application's configuration

I've read several docs about how to load properties into a SpringBootTest, but just haven't had any sort of luck.

Thanks in advance.


edit: src/main/resources/bootstrap.yml file:

---
spring:
  application:
    name: MyApp
  cloud:
    config:
      uri: {pathToDevelopmentConfigServer}
      enabled: false
0

There are 0 best solutions below