【黑马程序员SpringBoot2全套视频教程,springboot零基础到项目实战(spring boot2完整版)】
创建一个全新的模块工程

依赖都不勾


OK, 一个全新的SpringBoot 工程
把原有的测试类直接删掉

自己加一个

以前我们都是直接往配置文件中写属性,我现在的需求是,仅仅在我自己的测试类中加上一些属性
【先在配置文件中搞】
test:
prop: testValue

在测试中读取
package com.dingjiaxiong;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
/**
* ClassName: PropertiesAndArgsTest
* date: 2022/10/19 13:30
*
* @author DingJiaxiong
*/
@SpringBootTest
public class PropertiesAndArgsTest {
@Value("${test.prop}")
private String msg;
@Test
void testProperties(){
System.out.println(msg);
}
}
测试结果

OK,没啥毛病
【添加临时属性】
现在把配置文件中的东西注掉

@SpringBootTest(properties = {"test.prop=testValue1"})

没毛病【现在这个属性就仅在这个测试类中有效了】
假如两边都有?

可以看到临时生效
【第二种方式】
package com.dingjiaxiong;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
/**
* ClassName: PropertiesAndArgsTest
* date: 2022/10/19 13:30
*
* @author DingJiaxiong
*/
//@SpringBootTest(properties = {"test.prop=testValue1"})
@SpringBootTest(args = {"--test.prop=testValue2"})
public class PropertiesAndArgsTest {
@Value("${test.prop}")
private String msg;
@Test
void testProperties() {
System.out.println(msg);
}
}
直接测

没毛病【这种方式也可以】
如果这仨都有??

换个位置

OK,properties 属性最大
这里和李老师的结果就不一样了,OK,新版本的SpringBoot 已经变成了pro 覆盖arg,2.5.4 那会儿是arg 覆盖 pro
验证一下:版本降低
这下就是arg 覆盖 pro了,好家伙
回顾一下

优势:比多环境开发中的测试环境影响范围更小,仅对当前测试类有效
