我需要读取一个隐藏在我的包结构中的属性文件com.al.common.email.templates.
我已经尝试了一切,我无法弄明白.
最后,我的代码将在servlet容器中运行,但我不想依赖于容器.我编写JUnit测试用例,它需要兼顾两者.
我有一个jUnit测试,它有自己的属性文件(application-test.properties)和它的spring配置文件(application-core-test.xml).
其中一种方法使用spring config实例化的对象,它是一个spring组件.类中的一个成员从application.properties派生它的值,application.properties是我们的主要属性文件.通过jUnit访问此值时,它始终为null.我甚至尝试更改属性文件以指向实际的属性文件,但这似乎不起作用.
这是我访问属性文件对象的方式
@Component
@PropertySource("classpath:application.properties")
public abstract class A {
@Value("${test.value}")
public String value;
public A(){
SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);
}
public A(String text) {
this();
// do something with text and value.. here is where I run into NPE
}
}
public class B extends A {
//addtnl code
private B() {
}
private B(String text) {
super(text)
}
}
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:META-INF/spring/application-core-test.xml",
"classpath:META-INF/spring/application-schedule-test.xml"})
@PropertySource("classpath:application-test.properties")
public class TestD {
@Value("${value.works}")
public String valueWorks;
@Test
public void testBlah() {
SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);
B b= new …Run Code Online (Sandbox Code Playgroud)