Spring Boot 单元测试注入properties文件的属性

项目中想自定义一个properties文件存放支付相关的属性,并在单元测试中获取这个属性进行测试。

发现注入不成功,对此进行研究。

分析过程:

如下图所示在resources目录下创建一个pay.properties文件:

并在其中其中存放需要的key和value

然后开始编写单元测试类:

package com.pingxx.example;


import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
public class PayTest {

    static final Logger logger = LoggerFactory.getLogger(PayTest.class);

    @Value("${pay.apiKey}")
    private String apiKey;

    @Value("${pay.appId}")
    private String appId;

    @Value("${pay.privateKeyPath}")
    private String privateKeyPath;

    @Test
    public void valueTest(){
//        Assert.assertNotNull(apiKey);
        logger.debug(apiKey);
    }

}

发现日志系统打印出来的apiKey对应的值为:"${pay.apiKey}",显然不对。

估计是此时还没有加载配置文件,因此把pay.properties内的内容复制到application.properties试试,发现还不行。

 

搜了一下(http://www.baeldung.com/properties-with-spring)得到如下内容:

 

因此对代码进行修改:

package com.pingxx.example;


import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
public class PayTest {

    static final Logger logger = LoggerFactory.getLogger(PayTest.class);

    @Value("${pay.apiKey}")
    private String apiKey;

    @Value("${pay.appId}")
    private String appId;

    @Value("${pay.privateKeyPath}")
    private String privateKeyPath;

    @Test
    public void valueTest(){
//        Assert.assertNotNull(apiKey);
        logger.debug(apiKey);
    }

    @Configuration
    @PropertySource("classpath:pay.properties")
    static class PropertiesWithJavaConfig {

        @Bean
        public static PropertySourcesPlaceholderConfigurer
        propertySourcesPlaceholderConfigurer() {
            return new PropertySourcesPlaceholderConfigurer();
        }
    }

}

发现apiKe的值被正确输出。大功告成!

等等,作为追求完美的年轻人我们不能就此满足,继续查查官方文档,是否有更好的解决方案呢?

打开spring boot的官方参考手册(“spring-boot-reference”,点击可以下载)看看,能否有更好方法呢?

我们通过搜索“PropertySource”发现了如下内容:

啥?TestPropertySource,看这名字就应该是和测试相关的属性注解,看看后面的解释"annotations on your tests",果然!

我们删除上面的代码只新增一个注解看看

package com.pingxx.example;


import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@TestPropertySource("classpath:pay.properties")
public class PayTest {

    static final Logger logger = LoggerFactory.getLogger(PayTest.class);

    @Value("${pay.apiKey}")
    private String apiKey;

    @Value("${pay.appId}")
    private String appId;

    @Value("${pay.privateKeyPath}")
    private String privateKeyPath;

    @Test
    public void valueTest(){
//        Assert.assertNotNull(apiKey);
        logger.debug(apiKey);
    }



}

发现果真输出了我们需要的value.

建议:

最近发现官方的参考手册和GitHub代码和示例是最权威和最全面的参考文档。

建议不管学习什么技术,都要下载下来,没事的适合读读,遇到问题的适合多查查。

如果觉得本文对你有帮助,欢迎点赞,欢迎关注我,如果有补充欢迎评论交流,我将努力创作更多更好的文章。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

明明如月学长

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值