Spring jUnit测试属性文件

nqwrtyyt  于 2022-11-11  发布在  Spring
关注(0)|答案(4)|浏览(125)

我有一个jUnit Test,它有自己的属性文件(application-test.properties)和Spring配置文件(application-core-test.xml)。
其中一个方法使用了一个由spring config示例化的对象,它是一个spring组件。类中的一个成员从www.example.com中获得它的值application.properties,这是我们的主属性文件。当通过jUnit访问这个值时,它总是为空。我甚至尝试将属性文件改为指向实际的属性文件,但似乎不起作用。
下面是我如何访问属性文件对象

@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 B("blah");
        //...addtnl code

    }    
}
s2j5cfk0

s2j5cfk01#

首先,application.properties@PropertySource中的www.example.com应该读作application-test.properties,如果这是文件的名称(匹配这些内容很重要):

@PropertySource("classpath:application-test.properties ")

该文件应该位于/src/test/resources类路径下(根目录)。
我不明白为什么要将依赖项硬编码到一个名为application-test.properties的文件中。
通常的做法是在不同的类路径上使用相同名称的属性文件,根据是否运行测试来加载其中一个。
在典型布局的应用程序中,您将拥有:

src/test/resources/application.properties

src/main/resources/application.properties

然后像这样注射:

@PropertySource("classpath:application.properties")

更好的做法是在Spring上下文中将该属性文件公开为bean,然后将该bean注入到任何需要它的组件中。这样,您的代码中就不会充斥着对www.example.com的引用application.properties,您可以使用任何您想要的属性源。下面是一个示例:how to read properties file in spring project?

ipakzgxi

ipakzgxi2#

至于测试,您应该使用Spring 4.1中的,它将覆盖在其他位置定义的属性:

@TestPropertySource("classpath:application-test.properties")

测试属性源的优先级高于从操作系统环境或Java系统属性加载的属性源,以及由应用程序添加的属性源(如@PropertySource

pxy2qtax

pxy2qtax3#

我面临着同样的问题,花了太多的卡路里寻找正确的修复,直到我决定解决与文件阅读:

Properties configProps = new Properties();
InputStream iStream = new ClassPathResource("myapp-test.properties").getInputStream();
InputStream iStream = getConfigFile();
configProps.load(iStream);
wvyml7n5

wvyml7n54#

如果您在Docker容器中使用jar文件,并且资源属性文件,比如application.properties被打包在包含java的同一个classes目录中(这是IntelliJ IDE自动为存储在/src/main/resources中的资源文件所做的),这就是帮助我的地方:

public static Properties props = new Properties();

    static {
        try {
            props.load(
                    Thread
                            .currentThread()
                            .getContextClassLoader()
                            .getResourceAsStream("application.properties")
            );
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

大多数其他方法要么只在IDE内部工作,要么只在Docker内部工作。这一个方法在两者中都工作。

相关问题