前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Spring Boot 单元测试注入properties文件的属性

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

作者头像
明明如月学长
发布2021-08-27 16:14:36
2.4K0
发布2021-08-27 16:14:36
举报

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

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

分析过程:

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

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

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

代码语言:javascript
复制
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)得到如下内容:

因此对代码进行修改:

代码语言:javascript
复制
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",果然!

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

代码语言:javascript
复制
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代码和示例是最权威和最全面的参考文档。

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

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

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2018-04-19 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档