[笔记]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文件

总结


相关文章
|
8月前
|
测试技术 开发者 Python
Python单元测试入门:3个核心断言方法,帮你快速定位代码bug
本文介绍Python单元测试基础,详解`unittest`框架中的三大核心断言方法:`assertEqual`验证值相等,`assertTrue`和`assertFalse`判断条件真假。通过实例演示其用法,帮助开发者自动化检测代码逻辑,提升测试效率与可靠性。
557 1
|
10月前
|
Java Spring
Spring Boot配置的优先级?
在Spring Boot项目中,配置可通过配置文件和外部配置实现。支持的配置文件包括application.properties、application.yml和application.yaml,优先级依次降低。外部配置常用方式有Java系统属性(如-Dserver.port=9001)和命令行参数(如--server.port=10010),其中命令行参数优先级高于系统属性。整体优先级顺序为:命令行参数 &gt; Java系统属性 &gt; application.properties &gt; application.yml &gt; application.yaml。
1292 0
|
11月前
|
人工智能 Java 测试技术
Spring Boot 集成 JUnit 单元测试
本文介绍了在Spring Boot中使用JUnit 5进行单元测试的常用方法与技巧,包括添加依赖、编写测试类、使用@SpringBootTest参数、自动装配测试模块(如JSON、MVC、WebFlux、JDBC等),以及@MockBean和@SpyBean的应用。内容实用,适合Java开发者参考学习。
1158 0
|
7月前
|
JavaScript Java Maven
【SpringBoot(二)】带你认识Yaml配置文件类型、SpringMVC的资源访问路径 和 静态资源配置的原理!
SpringBoot专栏第二章,从本章开始正式进入SpringBoot的WEB阶段开发,本章先带你认识yaml配置文件和资源的路径配置原理,以方便在后面的文章中打下基础
581 4
|
7月前
|
Java 测试技术 数据库连接
【SpringBoot(四)】还不懂文件上传?JUnit使用?本文带你了解SpringBoot的文件上传、异常处理、组件注入等知识!并且带你领悟JUnit单元测试的使用!
Spring专栏第四章,本文带你上手 SpringBoot 的文件上传、异常处理、组件注入等功能 并且为你演示Junit5的基础上手体验
1106 3
|
7月前
|
XML Java 应用服务中间件
【SpringBoot(一)】Spring的认知、容器功能讲解与自动装配原理的入门,带你熟悉Springboot中基本的注解使用
SpringBoot专栏开篇第一章,讲述认识SpringBoot、Bean容器功能的讲解、自动装配原理的入门,还有其他常用的Springboot注解!如果想要了解SpringBoot,那么就进来看看吧!
705 2
|
8月前
|
Java 测试技术 数据安全/隐私保护
通过yaml文件配置自动化测试程序
通过yaml文件可以将自动化测试环境,测试数据和测试行为分开,请看一下案例
318 4
|
8月前
|
缓存 Java 应用服务中间件
Spring Boot配置优化:Tomcat+数据库+缓存+日志,全场景教程
本文详解Spring Boot十大核心配置优化技巧,涵盖Tomcat连接池、数据库连接池、Jackson时区、日志管理、缓存策略、异步线程池等关键配置,结合代码示例与通俗解释,助你轻松掌握高并发场景下的性能调优方法,适用于实际项目落地。
1453 5
|
9月前
|
XML Ubuntu Java
如何在Ubuntu系统上安装和配置JMeter和Ant进行性能测试
进入包含 build.xml 的目录并执行:
399 13
|
8月前
|
传感器 Java 数据库
探索Spring Boot的@Conditional注解的上下文配置
Spring Boot 的 `@Conditional` 注解可根据不同条件动态控制 Bean 的加载,提升应用的灵活性与可配置性。本文深入解析其用法与优势,并结合实例展示如何通过自定义条件类实现环境适配的智能配置。
424 0
探索Spring Boot的@Conditional注解的上下文配置

热门文章

最新文章