Java Spring jUnit 测试属性文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32974432/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 14:06:41  来源:igfitidea点击:

Spring jUnit Testing properties file

javaspringjunitspring-junit

提问by user1707141

I have a jUnit Test that has its own properties file(application-test.properties) and its spring config file(application-core-test.xml).

我有一个 jUnit 测试,它有自己的属性文件(application-test.properties)和它的 spring 配置文件(application-core-test.xml)。

One of the method uses an object instantiated by spring config and that is a spring component. One of the members in the classes derives its value from application.properties which is our main properties file. While accessing this value through jUnit it is always null. I even tried changing the properties file to point to the actual properties file, but that doesnt seem to work.

其中一种方法使用由 spring config 实例化的对象,即 spring 组件。类中的一个成员从 application.properties 派生它的值,它是我们的主要属性文件。通过 jUnit 访问此值时,它始终为 null。我什至尝试更改属性文件以指向实际的属性文件,但这似乎不起作用。

Here is how I am accessing the properties file object

这是我访问属性文件对象的方式

@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

    }    
}      

回答by Robert Moskal

Firstly, application.properties in the @PropertySource should read application-test.propertiesif that's what the file is named (matching these things up matters):

首先,@PropertySource 中的 application.properties 应该读取application-test.properties文件的名称(匹配这些内容很重要):

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

That file should be under your /src/test/resourcesclasspath (at the root).

该文件应该在您的/src/test/resources类路径下(在根目录下)。

I don't understand why you'd specify a dependency hard coded to a file called application-test.properties. Is that component only to be used in the test environment?

我不明白你为什么要指定一个硬编码到一个名为application-test.properties. 该组件仅用于测试环境吗?

The normal thing to do is to have property files with the same name on different classpaths. You load one or the other depending on whether you are running your tests or not.

正常的做法是在不同的类路径上拥有相同名称的属性文件。您加载一个或另一个取决于您是否正在运行测试。

In a typically laid out application, you'd have:

在典型布局的应用程序中,您将拥有:

src/test/resources/application.properties

and

src/main/resources/application.properties

And then inject it like this:

然后像这样注入它:

@PropertySource("classpath:application.properties")

The even better thing to do would be to expose that property file as a bean in your spring context and then inject that bean into any component that needs it. This way your code is not littered with references to application.properties and you can use anything you want as a source of properties. Here's an example: how to read properties file in spring project?

更好的做法是在 spring 上下文中将该属性文件作为 bean 公开,然后将该 bean 注入任何需要它的组件中。这样你的代码就不会充斥着对 application.properties 的引用,你可以使用任何你想要的作为属性源。这是一个例子:如何在spring项目中读取属性文件?

回答by sendon1982

As for the testing, you should use from Spring 4.1 which will overwrite the properties defined in other places:

至于测试,你应该从 Spring 4.1 开始使用,它会覆盖其他地方定义的属性:

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

Test property sources have higher precedence than those loaded from the operating system's environment or Java system properties as well as property sources added by the application like @PropertySource

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