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

java springboot在当前测试类中添加临时属性 不影响application和其他范围

目前 我们的属性基本都写在 application.yml 里面了
但是 如果 我们只是想做一下临时变量的测试 有没有办法实现呢?
显然是有的

这里 我们还是先在application.yml中去写一个 test属性 下面加个prop
在这里插入图片描述

然后 我们尝试在测试类中 获取一下这个属性

直接用 Value 读取属性 然后 在函数中输出就好了
在这里插入图片描述
然后 我们右键测试方法 选择运行
在这里插入图片描述
这样 我们的值就被读取到了
在这里插入图片描述
然后 我们在配置文件中 将这个内容去掉
在这里插入图片描述
这里 我们直接在测试类中 用SpringBootTest声明就好

package com.example.webdom;import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;@SpringBootTest(properties = {"test.prop=testValue1"})
public class WebDomApplicationTests {@Value("${test.prop}")private String Data;@Testvoid contextLoads() {System.out.println(Data);}}

在这里插入图片描述
然后 我们右键运行 这里 也是被正确输出了
在这里插入图片描述
然后 如果 application 和当前 配置的临时 都设置了同一个 用谁的?
临时属性优先级高
例如 都设置了 data 配置文件 1 当前测试类 临时属性 2 则 在当前测试类拿到的是 2

然后 临时属性 还有一种写法

我们将测试类代码改成这样

package com.example.webdom;import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;@SpringBootTest(args = {"--test.prop=testValue2"})
public class WebDomApplicationTests {@Value("${test.prop}")private String Data;@Testvoid contextLoads() {System.out.println(Data);}}

args 注意 属性名 前面要加 –
运行之后 输出正常
在这里插入图片描述
然后 这时 大家都会想花活了 args 和 properties同时设置了 谁的优先级高?
args优先级高啊 直接回答了

http://www.lryc.cn/news/234341.html

相关文章:

  • 原型网络Prototypical Network的python代码逐行解释,新手小白也可学会!!由于工作量大,准备整8个系列完事,-----系列5
  • milvus数据库的数据管理-插入数据
  • 系列一、请谈谈你对JVM的理解?Java8的虚拟机有什么更新?
  • 恕我直言,大模型对齐可能无法解决安全问题,我们都被表象误导了
  • Apache Airflow (九) :Airflow Operators及案例之BashOperator及调度Shell命令及脚本
  • IJ中配置TortoiseSVN插件:
  • 个人实现在线支付,一种另类的在线支付解决方案
  • 浅谈智能安全配电装置应用在银行配电系统中
  • macOS下如何使用Flask进行开发
  • 记一次服务器配置文件获取OSS
  • 合众汽车选用风河Wind River Linux系统
  • PTA平台-2023年软件设计综合实践_5(指针及引用)
  • 智慧卫生间
  • Cadence virtuoso drc lvs pex 无法输入
  • 反序列化漏洞(2), 分析调用链, 编写POC
  • Pytorch reshape用法
  • Latex 辅助写作工具
  • frp新版本frp_0.52.3设置
  • 100G.的DDoS高防够用吗?
  • 【django+vue】项目搭建、解决跨域访问
  • 【数据库】数据库连接池导致系统吞吐量上不去-复盘
  • 华纳云:租用的服务器连接超时怎么办?
  • 基于MS16F3211芯片的触摸控制灯的状态变化和亮度控制(11.17,PWM)
  • 编译buildroot出错,这个怎么解决呢,感谢
  • 【0基础学Java第十课】-- 认识String类
  • lxml基本使用
  • 【数据结构初阶】链表OJ
  • 【Vue渲染】 条件渲染 | v-if | v-show | 列表渲染 | v-for
  • 开源网安解决方案荣获四川数实融合创新实践优秀案例
  • debian/ubuntu/linux如何快速安装vscode