【发布时间】:2018-12-18 18:21:05
【问题描述】:
场景
我有一个带有 @Configuration 注释 Spring 配置类的 Spring Boot 应用程序,其中包含一些 @Value 注释字段。为了测试,我想用自定义测试值替换这些字段值。
不幸的是,这些测试值不能使用简单的属性文件、(字符串)常量或类似文件来覆盖,而是我必须使用一些自定义的写入属性来解析 Java 类(例如 TargetProperties.getProperty("some.username"))。
我遇到的问题是,当我在测试配置中将自定义 PropertySource 添加到 ConfigurableEnvironment 时,已经太晚了,因为这个 PropertySource 将被添加 在 例如RestTemplate 已创建。
问题
我怎样才能覆盖@Value 带注释的字段在 @Configuration 类中以编程方式 通过自定义Java 代码获得的属性之前被初始化了吗?
代码
生产配置类
@Configuration
public class SomeConfiguration {
@Value("${some.username}")
private String someUsername;
@Value("${some.password}")
private String somePassword;
@Bean
public RestTemplate someRestTemplate() {
RestTemplate restTemplate = new RestTemplate();
restTemplate.getInterceptors().add(
new BasicAuthorizationInterceptor(someUsername, somePassword));
return restTemplate;
}
}
测试配置类
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE)
public class SomeTest {
@SpringBootConfiguration
@Import({MySpringBootApp.class, SomeConfiguration.class})
static class TestConfiguration {
@Autowired
private ConfigurableEnvironment configurableEnvironment;
// This doesn't work:
@Bean
@Lazy(false)
// I also tried a @PostConstruct method
public TargetPropertiesPropertySource targetPropertiesPropertySource() {
TargetPropertiesPropertySource customPropertySource =
new TargetPropertiesPropertySource();
configurableEnvironment.getPropertySources().addFirst(customPropertySource);
return customPropertySource;
}
}
}
【问题讨论】:
标签: testing spring-boot