Spring Boot 单元测试注入properties文件的属性 - 明明如月的专栏 - CSDN博客

项目中想自定义一个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代码和示例是最权威和最全面的参考文档。

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

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

Original url: Access
Created at: 2019-08-08 17:39:49
Category: default
Tags: none

请先后发表评论
  • 最新评论
  • 总共0条评论