当前位置: 首页 > news >正文

SpringBoot加载测试类属性和配置说明

一、项目准备

1.创建项目

2.配置yml文件

test:name: FOREVERlove: sing

 二、测试类属性

1.@Value

说明:读取yml中的数据。

package com.forever;import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;@SpringBootTest
class Springboot10TestApplicationTests {@Value("${test.name}")private  String name;@Testvoid contextLoads() {System.out.println(name);}}

2.@SpringBootTest

说明:@SpringBootTest(properties = "test.name=Good");单文件临时生效。@SpringBootTest(args = {"--test.name=Better"})

package com.forever;import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;@SpringBootTest(properties = "test.name=Good")
class Springboot10TestApplicationTests {@Value("${test.name}")private  String name;@Testvoid contextLoads() {System.out.println(name);}}

 

 

package com.forever;import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;@SpringBootTest(args = {"--test.name=Better"})
class Springboot10TestApplicationTests {@Value("${test.name}")private  String name;@Testvoid contextLoads() {System.out.println(name);}}
 
@SpringBootTest(properties = {"test.name=Good"},args = {"--test.name=Better"})

说明:命令行的优先级高于其他的;args覆盖properties。

 三、测试类配置

1.@Import

说明:导入配置类,@Import(MsgConfig.class)。

1.1创建配置类

说明:开发中,不能将方法通过@Bean变成bean类。

package com.forever.config;import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;@Configuration
public class MsgConfig {//开发误用@Beanpublic String msg(){return "bean msg";}
}

1.2测试类

说明:通过@Import追加配置。

package com.forever;import com.forever.config.MsgConfig;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.Import;@SpringBootTest
//追加了配置
@Import(MsgConfig.class)
class Springboot10TestApplicationTests {@Autowiredprivate String msg;@Testvoid contextLoads() {System.out.println(msg);}}

相关文章:

  • git 提交代码,但是有些文件没有上传
  • 【论文阅读】PSDF Fusion:用于动态 3D 数据融合和场景重建的概率符号距离函数
  • Kotlin基础数据类型和运算符
  • Python的计算性能优化
  • QTabBar实验
  • 动态轮换住宅代理是什么?为何需要使用它?
  • 海思SD3403/SS928开发板 开发记录二: 设置网络 telnet连接开发板
  • mybatis-generator生成CURD
  • 52基于MATLAB的希尔伯特Hilbert变换求包络谱
  • CMake中的变量: 改变构建行为的变量
  • 软件测试用例与分类
  • 缓存-基础理论和Guava Cache介绍
  • spring面试题笔记
  • 行为型模式-命令模式
  • 入门 对有序数组进行二分搜索 + 图解 (下篇)
  • JS中 map, filter, some, every, forEach, for in, for of 用法总结
  • 11111111
  • Bootstrap JS插件Alert源码分析
  • Java方法详解
  • java概述
  • js中的正则表达式入门
  • Laravel核心解读--Facades
  • linux安装openssl、swoole等扩展的具体步骤
  • October CMS - 快速入门 9 Images And Galleries
  • spring-boot List转Page
  • Spring-boot 启动时碰到的错误
  • vue+element后台管理系统,从后端获取路由表,并正常渲染
  • vue-router 实现分析
  • 回顾2016
  • 开年巨制!千人千面回放技术让你“看到”Flutter用户侧问题
  • 快速构建spring-cloud+sleuth+rabbit+ zipkin+es+kibana+grafana日志跟踪平台
  • 利用jquery编写加法运算验证码
  • 排序(1):冒泡排序
  • 浅谈web中前端模板引擎的使用
  • 容器服务kubernetes弹性伸缩高级用法
  • 限制Java线程池运行线程以及等待线程数量的策略
  • $(selector).each()和$.each()的区别
  • (14)学习笔记:动手深度学习(Pytorch神经网络基础)
  • (博弈 sg入门)kiki's game -- hdu -- 2147
  • (超详细)2-YOLOV5改进-添加SimAM注意力机制
  • (附表设计)不是我吹!超级全面的权限系统设计方案面世了
  • (附源码)springboot 房产中介系统 毕业设计 312341
  • (机器学习的矩阵)(向量、矩阵与多元线性回归)
  • (转)Android学习系列(31)--App自动化之使用Ant编译项目多渠道打包
  • (转)C#调用WebService 基础
  • (转)编辑寄语:因为爱心,所以美丽
  • ***php进行支付宝开发中return_url和notify_url的区别分析
  • .NET Core WebAPI中使用swagger版本控制,添加注释
  • .Net Core缓存组件(MemoryCache)源码解析
  • .net 反编译_.net反编译的相关问题
  • .NET 应用启用与禁用自动生成绑定重定向 (bindingRedirect),解决不同版本 dll 的依赖问题
  • .NET/C# 异常处理:写一个空的 try 块代码,而把重要代码写到 finally 中(Constrained Execution Regions)
  • .NET版Word处理控件Aspose.words功能演示:在ASP.NET MVC中创建MS Word编辑器
  • @Transactional 详解
  • [Bzoj4722]由乃(线段树好题)(倍增处理模数小快速幂)