@Value 注释在应用程序中解析,但在单元测试中不解析
@Value annotation resolves in app but not in unit test
我有一个 Spring 带有 @Value 注释的 Web 应用程序,由 PropertySourcesPlaceholderConfigurer 填充。注释在我的网络应用程序中正确解析。
但是,当我 运行 使用 SpringJunit4ClassRunner 和单元测试的特定 bean 配置文件进行单元测试时,我得到一个 "unable to resolve placeholder xxx in string ${xxx}" 异常,无论我 运行 来自 Eclipse 或 Maven 的测试。
有趣的是,PropertySourcesPlcHlCfg。被初始化(正确且只有一次)并找到属性文件,就像我重命名它一样,我得到一个 FileNotFoundException。
这里可能出了什么问题?
谢谢!
您可以尝试使用(如果是Spring 4)
@TestPropertySource(value="classpath:/test.properties")
用于为 Junit 测试加载特定文件。
或者,您也可以尝试在配置中创建一个 bean PropertySourcesPlaceholderConfigurer
@Bean
public static PropertySourcesPlaceholderConfigurer propertiesResolver() {
return new PropertySourcesPlaceholderConfigurer();
}
我有一个 Spring 带有 @Value 注释的 Web 应用程序,由 PropertySourcesPlaceholderConfigurer 填充。注释在我的网络应用程序中正确解析。
但是,当我 运行 使用 SpringJunit4ClassRunner 和单元测试的特定 bean 配置文件进行单元测试时,我得到一个 "unable to resolve placeholder xxx in string ${xxx}" 异常,无论我 运行 来自 Eclipse 或 Maven 的测试。
有趣的是,PropertySourcesPlcHlCfg。被初始化(正确且只有一次)并找到属性文件,就像我重命名它一样,我得到一个 FileNotFoundException。
这里可能出了什么问题?
谢谢!
您可以尝试使用(如果是Spring 4)
@TestPropertySource(value="classpath:/test.properties")
用于为 Junit 测试加载特定文件。
或者,您也可以尝试在配置中创建一个 bean PropertySourcesPlaceholderConfigurer
@Bean
public static PropertySourcesPlaceholderConfigurer propertiesResolver() {
return new PropertySourcesPlaceholderConfigurer();
}