测试
加载测试专用属性
模拟案例
新建项目
什么都不勾
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-OGvtO8hm-1653120593764)(springboot.assets/image-20220521153838531.png)]](https://img-blog.csdnimg.cn/a3f50e1b60e94ab49b5e4125bfd9e017.png)
新建PropertiesAndArgsTest.java
package com.taotao;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
@SuppressWarnings({"all"})
@SpringBootTest
public class PropertiesAndArgsTest {
@Value("${test.prop}")
private String msg;
@Test
void testProperties(){
System.out.println(msg);
}
}
编辑yml
test:
prop: testValue
测试运行
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-8hGUEdIT-1653120593766)(springboot.assets/image-20220521154622785.png)]](https://img-blog.csdnimg.cn/a7cb76654b0f4a89b051bc9697eb0a76.png)
模拟案例(第二种形式)
直接注释yml文件
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-FIuqN6Pv-1653120593766)(springboot.assets/image-20220521154805736.png)]](https://img-blog.csdnimg.cn/ccad48bc24534642aa285a5d0424853a.png)
在springBootTest注解里声明
测试成功
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-6bf7hCNg-1653120593767)(springboot.assets/image-20220521154852876.png)]](https://img-blog.csdnimg.cn/b017e48cbdc04a4486d0eafb01a098e4.png)
模拟案例(yml和注解都存在)
yml
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-DZPuaCov-1653120593768)(springboot.assets/image-20220521155030413.png)]](https://img-blog.csdnimg.cn/29407ef4cc0048e98843f53bc9dd9745.png)
测试运行
就近原则
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-oAn6GIrj-1653120593769)(springboot.assets/image-20220521155100033.png)]](https://img-blog.csdnimg.cn/a136cc7056324b0487ded4568fe41759.png)
模拟案例(args数组参数)
args属性可以为当前测试用例添加临时的命令行参数
编辑PropertiesAndArgsTest
package com.taotao;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
@SuppressWarnings({"all"})
@SpringBootTest(args = {"--test.prop=testValue1"})
public class PropertiesAndArgsTest {
@Value("${test.prop}")
private String msg;
@Test
void testProperties(){
System.out.println(msg);
}
}
模拟案例(args,properties,yml整合)
yml
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-RGVBvAhQ-1653120593770)(springboot.assets/image-20220521155751576.png)]](https://img-blog.csdnimg.cn/39efdc610e5d44e4ad5040521154bca6.png)
package com.taotao;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
@SuppressWarnings({"all"})
@SpringBootTest(args = {"--test.prop=testValue2"},
properties = {"test.prop=testValue1"})
public class PropertiesAndArgsTest {
@Value("${test.prop}")
private String msg;
@Test
void testProperties(){
System.out.println(msg);
}
}
测试结果
SpringBootTest注解后面,谁最后加载
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-pSk6zw5K-1653120593771)(springboot.assets/image-20220521160015217.png)]](https://img-blog.csdnimg.cn/a715649cab9240bf8d6bbb26db2aa403.png)
小结
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-9jRup6YB-1653120593772)(springboot.assets/image-20220521160118448.png)]](https://img-blog.csdnimg.cn/df5bd447b19047afb2c000a6fa4462b8.png)
|