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

总结


相关文章
|
测试技术 开发者 Python
Python单元测试入门:3个核心断言方法,帮你快速定位代码bug
本文介绍Python单元测试基础,详解`unittest`框架中的三大核心断言方法:`assertEqual`验证值相等,`assertTrue`和`assertFalse`判断条件真假。通过实例演示其用法,帮助开发者自动化检测代码逻辑,提升测试效率与可靠性。
814 1
|
缓存 Java API
微服务——SpringBoot使用归纳——Spring Boot集成 Swagger2 展现在线接口文档——Swagger2 的配置
本文介绍了在Spring Boot中配置Swagger2的方法。通过创建一个配置类,添加`@Configuration`和`@EnableSwagger2`注解,使用Docket对象定义API文档的详细信息,包括标题、描述、版本和包路径等。配置完成后,访问`localhost:8080/swagger-ui.html`即可查看接口文档。文中还提示了可能因浏览器缓存导致的问题及解决方法。
1664 0
微服务——SpringBoot使用归纳——Spring Boot集成 Swagger2 展现在线接口文档——Swagger2 的配置
|
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。
1574 0
|
Java 关系型数据库 数据库
微服务——SpringBoot使用归纳——Spring Boot事务配置管理——Spring Boot 事务配置
本文介绍了 Spring Boot 中的事务配置与使用方法。首先需要导入 MySQL 依赖,Spring Boot 会自动注入 `DataSourceTransactionManager`,无需额外配置即可通过 `@Transactional` 注解实现事务管理。接着通过创建一个用户插入功能的示例,展示了如何在 Service 层手动抛出异常以测试事务回滚机制。测试结果表明,数据库中未新增记录,证明事务已成功回滚。此过程简单高效,适合日常开发需求。
2186 0
|
Java 测试技术 微服务
微服务——SpringBoot使用归纳——Spring Boot中的项目属性配置——少量配置信息的情形
本课主要讲解Spring Boot项目中的属性配置方法。在实际开发中,测试与生产环境的配置往往不同,因此不应将配置信息硬编码在代码中,而应使用配置文件管理,如`application.yml`。例如,在微服务架构下,可通过配置文件设置调用其他服务的地址(如订单服务端口8002),并利用`@Value`注解在代码中读取这些配置值。这种方式使项目更灵活,便于后续修改和维护。
585 0
|
JavaScript Java Maven
【SpringBoot(二)】带你认识Yaml配置文件类型、SpringMVC的资源访问路径 和 静态资源配置的原理!
SpringBoot专栏第二章,从本章开始正式进入SpringBoot的WEB阶段开发,本章先带你认识yaml配置文件和资源的路径配置原理,以方便在后面的文章中打下基础
827 4
|
XML Java 应用服务中间件
【SpringBoot(一)】Spring的认知、容器功能讲解与自动装配原理的入门,带你熟悉Springboot中基本的注解使用
SpringBoot专栏开篇第一章,讲述认识SpringBoot、Bean容器功能的讲解、自动装配原理的入门,还有其他常用的Springboot注解!如果想要了解SpringBoot,那么就进来看看吧!
1071 2
|
Java 测试技术 数据安全/隐私保护
通过yaml文件配置自动化测试程序
通过yaml文件可以将自动化测试环境,测试数据和测试行为分开,请看一下案例
565 4
|
缓存 Java 应用服务中间件
Spring Boot配置优化:Tomcat+数据库+缓存+日志,全场景教程
本文详解Spring Boot十大核心配置优化技巧,涵盖Tomcat连接池、数据库连接池、Jackson时区、日志管理、缓存策略、异步线程池等关键配置,结合代码示例与通俗解释,助你轻松掌握高并发场景下的性能调优方法,适用于实际项目落地。
2222 5
|
XML Ubuntu Java
如何在Ubuntu系统上安装和配置JMeter和Ant进行性能测试
进入包含 build.xml 的目录并执行:
711 13