【问题标题】:Spring jUnit Testing properties fileSpring jUnit 测试属性文件
【发布时间】:2016-01-03 15:13:31
【问题描述】:

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

其中一种方法使用由 spring config 实例化的对象,即 spring 组件。类中的成员之一从我们的主要属性文件 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

    }    
}      

【问题讨论】:

  • 如何实例化A 的实例?我的猜测是您正在使用 new 而不是从 ApplicationContext 查找它
  • 那么@PropertySource 中的application.properties 不应该读取application-test.properties 吗?
  • @Lance Java:是的,我正在使用新的。将尝试从 ApplicationContext 查找..
  • @RobertMoskal 我试过了,但似乎也没有用。
  • 您需要@ComponentScan 或<context:component-scan/> 来获取您的@Component

标签: java spring junit spring-junit


【解决方案1】:

我遇到了同样的问题,花了太多卡路里寻找正确的解决方法,直到我决定安顿下来阅读文件:

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

【讨论】:

    【解决方案2】:

    首先,@PropertySource 中的 application.properties 应为 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 注入到任何需要它的组件中。这样,您的代码就不会被对 application.properties 的引用所困扰,并且您可以使用任何您想要的东西作为属性的来源。这是一个例子:how to read properties file in spring project?

    【讨论】:

    • 我已经进行了建议的更改,但它仍然没有从属性文件中加载值...我缺少什么
    • 你的类路径设置了吗?你如何运行它?
    • 是的,类路径已设置。上面 D 类中的代码提到了它是如何设置的。我正在使用 eclipse,我运行为 -> jUnit test
    【解决方案3】:

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

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

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

    【讨论】:

    • 谢谢!在 spring-boot 2.1.2 中,@TestPropertySource 在 JUnit 测试中与 @ContextConfiguration 一起使用,而 @PropertySource 则不行。
    猜你喜欢
    • 1970-01-01
    • 2013-08-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-26
    • 2018-10-08
    • 2014-12-25
    相关资源
    最近更新 更多