Неверный файл свойств SpringBoot, загруженный в тесте JUnit5 с @ActivateProfiles - PullRequest
0 голосов
/ 05 февраля 2020

Я пытаюсь протестировать файл конфигурации с другим профилем, используя шаблон имени файла конфигурации Spring.

Вот структура проекта Spring:

src/main
    java/foo/bar
        config
            RestClientConfig.java
            ApiProperties.java
    resources
        application.yml
        application-local.yml

application.yml

api:
  authentication-hash: fds456f4ds6f
  rest-api-uri: https://example.org/api

application-local.yml

api:
  authentication-hash: xxxxxxxxx
  rest-api-uri: https://localhost:8081/api

ApiProperties. java

@Configuration
@ConfigurationProperties(prefix = "boond")
@Data
public class BoondProperties {

    @ToString.Exclude // confidentiel
    private String authenticationHash;
    private URI restApiUri;

}

Конфигурация шаблона отдыха

@Configuration
public class RestClientConfig {

    private final ApiProperties apiProperties;

    public RestClientConfig(apiProperties apiProperties) {
        this.apiProperties = apiProperties;
    }

    @Bean
    public RestTemplate restTemplate(RestTemplateBuilder builder) {

        builder.defaultHeader("Authorization", "Basic " +
                apiProperties.getAuthenticationHash());
        builder.uriTemplateHandler(
                new DefaultUriBuilderFactory(apiProperties.getRestApiUri().toString()));
        return builder.build();
    }

}

Когда я запускаю следующий модуль тест с Junit5, загружает свойства из application.yml вместо application-local.yml

@SpringBootTest
@ActiveProfiles("local")
class RestClientConfigTest {
    @Autowired
    RestClientConfig restClientConfig;
    @Autowired
    RestTemplateBuilder restTemplateBuilder;

    @Test
    void rest_client_should_be_able_to_contact_api() {
        RestTemplate apiRestTemplate = restClientConfig.restTemplate(restTemplateBuilder);
        ResponseEntity<String> dictionary = apiRestTemplate.getForEntity("/api/dictionary", String.class);
        assertEquals("OK", dictionary);
    }
}

1 Ответ

0 голосов
/ 06 февраля 2020

На основании документации настройка конфигурации yaml c для профиля выполняется по-другому. Ниже приведено соглашение для .properties файлов.

Пожалуйста, обратитесь к общей документации

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...