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

简介: [笔记]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>

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 );
    }
}

读取配置yml/properties

方法一 @PropertySource + properties

aliyun-config.properties文件

aliyun.accessKey=123
aliyun.accessSecret=123
aliyun.iotInstanceId=123
aliyun.clientId=123
aliyun.host=123

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;
    }
}

方法二 @PropertySource + yml + YamlPropertySourceFactory

aliyun:
  accessKey: 123
  accessSecret: 123
  iotInstanceId: 123
  clientId: 123
  host: 123

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 = "xxxxxx";
    public static String accessKey = "";
    public static String accessSecret = "xxxxxx";
    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;
    }
}

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);
    }
}

方法三 @ConfigurationProperties + yml

方法四 @ActiveProfiles + yml

参考

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

总结


相关文章
|
4天前
|
测试技术 Python
|
10天前
|
NoSQL 前端开发 Java
技术笔记:springboot分布式锁组件spring
技术笔记:springboot分布式锁组件spring
13 1
|
2天前
|
JavaScript Java 测试技术
基于SpringBoot+Vue+uniapp的高中信息技术课程在线测试系统的详细设计和实现(源码+lw+部署文档+讲解等)
基于SpringBoot+Vue+uniapp的高中信息技术课程在线测试系统的详细设计和实现(源码+lw+部署文档+讲解等)
8 0
|
4天前
|
测试技术 数据安全/隐私保护 索引
基于SpringBoot+Vue大学生体质测试管理系统【源码+论文+演示视频+包运行成功】(2)
基于SpringBoot+Vue大学生体质测试管理系统【源码+论文+演示视频+包运行成功】
9 0
|
4天前
|
Java 关系型数据库 MySQL
基于SpringBoot+Vue大学生体质测试管理系统【源码+论文+演示视频+包运行成功】(1)
基于SpringBoot+Vue大学生体质测试管理系统【源码+论文+演示视频+包运行成功】
15 0
|
6天前
|
Java jenkins 持续交付
Jenkins是开源CI/CD工具,用于自动化Java项目构建、测试和部署。通过配置源码管理、构建触发器、执行Maven目标,实现代码提交即触发构建和测试
【7月更文挑战第1天】Jenkins是开源CI/CD工具,用于自动化Java项目构建、测试和部署。通过配置源码管理、构建触发器、执行Maven目标,实现代码提交即触发构建和测试。成功后,Jenkins执行部署任务,发布到服务器或云环境。使用Jenkins能提升效率,保证软件质量,加速上线,并需维护其稳定运行。
35 0
|
9天前
|
IDE Java 测试技术
Springboot单元测试步骤
Springboot单元测试步骤
11 0
|
9天前
|
缓存 Java 测试技术
Spring Boot中的性能测试与调优
Spring Boot中的性能测试与调优
|
9天前
|
Java 测试技术 持续交付
如何使用Spring Boot进行单元测试
如何使用Spring Boot进行单元测试
|
10天前
|
JSON Java API
技术笔记:springboot项目使用拦截器实现一个简单的网关请求透传
技术笔记:springboot项目使用拦截器实现一个简单的网关请求透传
16 0