• [笔记]Springboot入门《五》之单元测试读取配置


    前言

    • junit载入类
    • 配置类读取配置文件
      • .properties
      • .yml

    实现

    junit测试

    pom文件

    <dependencies>
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>4.11</version>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
            <!-- 自定义配置需要的依赖 -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-configuration-processor</artifactId>
                <optional>true</optional>
            </dependency>
        </dependencies>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    AppTest.java文件

    import static org.junit.Assert.assertTrue;
    
    import com.ruoyi.iot.aliyun.config.AliyunUserConstant;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.boot.test.context.SpringBootTest;
    import org.springframework.test.context.junit4.SpringRunner;
    
    /**
     * Unit test for simple App.
     */
    @RunWith(SpringRunner.class)
    @SpringBootTest(classes = AliyunUserConstant.class)
    public class AppTest
    {
        /**
         * Rigorous Test :-)
         */
        @Test
        public void shouldAnswerWithTrue()
        {
            System.out.println(AliyunUserConstant.accessKey);
            assertTrue( true );
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27

    读取配置yml/properties

    方法一 @PropertySource + properties

    aliyun-config.properties文件

    aliyun.accessKey=123
    aliyun.accessSecret=123
    aliyun.iotInstanceId=123
    aliyun.clientId=123
    aliyun.host=123
    
    • 1
    • 2
    • 3
    • 4
    • 5

    AliyunUserConstant.java文件

    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.boot.context.properties.ConfigurationProperties;
    import org.springframework.context.annotation.PropertySource;
    import org.springframework.stereotype.Component;
    
    @Component
    @PropertySource(value = {"classpath:aliyun-config.properties"})
    public class AliyunUserConstant {
        public static String regionId = "";
        //public static String accessKey = "";
        public static String accessKey = "";
        public static String accessSecret = "";
    
        public static String productKey = "";
        public static String deviceName = "";
        public static String instanceId = "";
    
        public static String clientId = "";
        public static String host = "";
    
        @Value("${aliyun.regionId}")
        public void setRegionId(String regionId) {
            AliyunUserConstant.regionId = regionId;
        }
    
        @Value("${aliyun.accessKey}")
        public void setAccessKey(String accessKey) {
            AliyunUserConstant.accessKey = accessKey;
        }
    
        @Value("${aliyun.accessSecret}")
        public void setAccessSecret(String accessSecret) {
            AliyunUserConstant.accessSecret = accessSecret;
        }
    
        @Value("${aliyun.productKey}")
        public void setProductKey(String productKey) {
            AliyunUserConstant.productKey = productKey;
        }
    
        @Value("${aliyun.deviceName}")
        public void setDeviceName(String deviceName) {
            AliyunUserConstant.deviceName = deviceName;
        }
    
        @Value("${aliyun.instanceId}")
        public void setInstanceId(String instanceId) {
            AliyunUserConstant.instanceId = instanceId;
        }
    
        @Value("${aliyun.clientId}")
        public void setClientId(String clientId) {
            AliyunUserConstant.clientId = clientId;
        }
    
        @Value("${aliyun.host}")
        public void setHost(String host) {
            AliyunUserConstant.host = host;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60

    方法二 @PropertySource + yml + YamlPropertySourceFactory

    aliyun:
      accessKey: 123
      accessSecret: 123
      iotInstanceId: 123
      clientId: 123
      host: 123
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    AliyunUserConstant .java文件

    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.boot.context.properties.ConfigurationProperties;
    import org.springframework.context.annotation.PropertySource;
    import org.springframework.stereotype.Component;
    
    @Component
    //以下ok
    @PropertySource(value = {"classpath:application-aliyun-config.yml"},factory = YamlPropertySourceFactory.class)
    public class AliyunUserConstant {
        public static String regionId = "cn-shanghai";
        //public static String accessKey = "LTAI5tP4G9fobpyPFUughQaZ";
        public static String accessKey = "";
        public static String accessSecret = "gNbmPE7AqfUvoDZdSXcduzvCBhLJc6";
    
        public static String productKey = "hf3fsfEX3CF";
        public static String deviceName = "test_device";
        public static String instanceId = "iot-06z00fu20bxiv4s";
    
        public static String clientId = "123456";
        public static String host = "iot-06z00fu20bxiv4s.amqp.iothub.aliyuncs.com";
    
        @Value("${aliyun.regionId}")
        public void setRegionId(String regionId) {
            AliyunUserConstant.regionId = regionId;
        }
    
        @Value("${aliyun.accessKey}")
        public void setAccessKey(String accessKey) {
            AliyunUserConstant.accessKey = accessKey;
        }
    
        @Value("${aliyun.accessSecret}")
        public void setAccessSecret(String accessSecret) {
            AliyunUserConstant.accessSecret = accessSecret;
        }
    
        @Value("${aliyun.productKey}")
        public void setProductKey(String productKey) {
            AliyunUserConstant.productKey = productKey;
        }
    
        @Value("${aliyun.deviceName}")
        public void setDeviceName(String deviceName) {
            AliyunUserConstant.deviceName = deviceName;
        }
    
        @Value("${aliyun.instanceId}")
        public void setInstanceId(String instanceId) {
            AliyunUserConstant.instanceId = instanceId;
        }
    
        @Value("${aliyun.clientId}")
        public void setClientId(String clientId) {
            AliyunUserConstant.clientId = clientId;
        }
    
        @Value("${aliyun.host}")
        public void setHost(String host) {
            AliyunUserConstant.host = host;
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62

    YamlPropertySourceFactory.java文件

    import org.springframework.boot.env.YamlPropertySourceLoader;
    import org.springframework.core.env.PropertySource;
    import org.springframework.core.io.support.EncodedResource;
    import org.springframework.core.io.support.PropertySourceFactory;
    
    import java.io.IOException;
    
    /**
     * @author Erwin Feng
     * @since 2020/8/13
     */
    public class YamlPropertySourceFactory implements PropertySourceFactory {
        @Override
        public PropertySource<?> createPropertySource(String name, EncodedResource resource) throws IOException {
            // 返回 yaml 属性资源
            return new YamlPropertySourceLoader()
                    .load(resource.getResource().getFilename(), resource.getResource())
                    .get(0);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    方法三 @ConfigurationProperties + yml

    方法四 @ActiveProfiles + yml

    参考

    @ActiveProfiles(value = "aliyun-config") // 载入application-aliyun-config.yml文件
    
    • 1

    总结

  • 相关阅读:
    A1S65B-S1 A1S61PN A1SJ51T64 机器人的优点和缺点
    小流域洪水分析模拟预报设计及代码实现
    怎么样让别人看不懂你的 JS 代码?
    Bootstrap 中CSS媒体查询分辨率 @media(min-width)
    我的数学学习回忆录——一个数学爱好者的反思(一)
    关于报错java.util.ConcurrentModificationException: null的源码分析和解决
    找到 K 个最接近的元素(java,算法)
    代码随想录第46天 | ● 583. 两个字符串的删除操作 ● 72. 编辑距离
    Prometheus Operator 与 kube-prometheus 之一-简介
    图论基础 —— 概述
  • 原文地址:https://blog.csdn.net/qq1113673178/article/details/126691904