SpringBootTest не импортирует локальные свойства

#java #spring-boot #junit #spring-boot-test

#java #spring-boot #junit #spring-boot-test

Вопрос:

Я запускаю полное тестирование приложения и хотел бы прочитать все свойства для моего приложения, src/test/resources но когда я добавляю @TestPropertySource местоположения и, приложение по-прежнему не загружается.

В рабочей среде моя конфигурация размещена на внешнем сервере, и я могу успешно загрузить конфигурацию для локального тестирования с помощью этого в boostrap.yml: spring.cloud.config.uri: {pathToDevelopmentConfigServer} Однако, когда я удаляю это в пользу попытки локального чтения конфигурации, я не могу загрузить приложение.

Моя настройка: тестовый класс:

 @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
@ConfigurationProperties(prefix = "test-config")
@Validated
public class MyAppProperties {

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

  // Getters amp; Setters
}
  

Файл конфигурации (MyApp-test.yaml):

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

  

Однако, когда я пытаюсь запустить это, я получаю эту ошибку:

 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
  

Я прочитал несколько документов о том, как загружать свойства в SpringBootTest, но просто не повезло.

Заранее спасибо.


редактировать: файл src/main/resources/bootstrap.yml:

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

Комментарии:

1. Здесь вам может не понадобиться TestPropertySource. Создайте простой ‘application.yaml’ в ‘src / test / resources’. Удалите все другие файлы yaml и аннотацию TestPropertySource. Он должен работать нормально.

2. Удалил @TestPropertySource и переместил всю конфигурацию в файл MyApp.yaml в src /test /resources. Никаких изменений в поведении