반응형
@동적 속성이 있는 TestPropertySource
봄 부팅 앱에 대한 통합 테스트에서 @TestPropertySource를 사용하여 application.yml 속성을 덮어씁니다.
@TestPropertySource(properties = { "repository.file.path=src/test/resources/x" })
저는 부동산 가치를 역동적으로 만드는 방법이 있는지 궁금합니다.이와 같은 것:
@TestPropertySource(properties = { "repository.file.path=PropertyValueProvider.class" })
귀하의 피드백에 감사합니다.내 경우 속성 값은 테스트 실행 시 생성되어야 하는 시스템별 값입니다.
@TestPropertySource
구성을 위한 선언적 메커니즘만 제공합니다.PropertySource
스프링 참조 매뉴얼의 설명서.
프로그램 추가 지원이 필요한 경우PropertySource
에게Environment
당신은 구현해야 합니다.ApplicationContextInitializer
를 통해 등록할 수 있습니다.@ContextConfiguration(initializers = ...)
스프링 참조 매뉴얼의 설명서.
안부 전해요,
샘(Spring Test Context Framework의 저자)
이 작업은 다음과 같이 수행할 수도 있습니다.@DynamicPropertySource
Spring Boot 5.2의 주석.이렇게 하면 동적으로 속성 값을 프로그래밍 방식으로 설정할 수 있습니다.
참조: https://github.com/spring-projects/spring-framework/issues/24540
통합 테스트 클래스에 추가합니다.
@DynamicPropertySource
static void dynamicProperties(DynamicPropertyRegistry registry) {
registry.add("my.property", () -> {
// some logic to get your property dynamically
});
}
샘플 코드는 다음과 같습니다.
@ContextConfiguration(classes = { MyConfiguration.class }, initializers = { MyInitializer.class })
public class MyContextConfiguration {
public static class MyInitializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {
@Override
public void initialize(ConfigurableApplicationContext applicationContext) {
String userNameLower = System.getProperty("user.name").toLowerCase();
Map<String, Object> dynamicProperties = Map.of("user.name.lower", userNameLower);
MapPropertySource propertySource = new MapPropertySource("dynamic", dynamicProperties);
applicationContext.getEnvironment().getPropertySources().addLast(propertySource);
}
}
@Configuration
@PropertySource("classpath:my-static.properties") // properties in this file can reference ${user.name.lower}
public static class MyConfiguration {
@Bean
public MyBean myBean(@Value("${user.name.lower}") String userNameLower) {
return new MyBean(userNameLower);
}
}
}
언급URL : https://stackoverflow.com/questions/33855874/testpropertysource-with-dynamic-properties
반응형
'programing' 카테고리의 다른 글
mongodbc# 드라이버와 함께 POCO를 사용할 때 _id 필드를 관리하는 방법 (0) | 2023.06.29 |
---|---|
64비트에서 32비트 부호 없는 곱셈이 정의되지 않은 동작을 유발합니까? (0) | 2023.06.29 |
AES 암호화 데이터를 통한 Mysql/MariaDB 검색 (0) | 2023.06.29 |
업데이트 색인 실행 취소 --작업 트리 건너뛰기 (0) | 2023.06.29 |
내 블록 크기 오라클 쿼리 (0) | 2023.06.29 |