相关疑难解决方法(0)

从Java包加载属性文件

我需要读取一个隐藏在我的包结构中的属性文件com.al.common.email.templates.

我已经尝试了一切,我无法弄明白.

最后,我的代码将在servlet容器中运行,但我不想依赖于容器.我编写JUnit测试用例,它需要兼顾两者.

java properties-file

109
推荐指数
5
解决办法
31万
查看次数

Spring 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)

java junit spring spring-junit

19
推荐指数
2
解决办法
10万
查看次数

标签 统计

java ×2

junit ×1

properties-file ×1

spring ×1

spring-junit ×1