- 测试
- 加载测试专用属性
- 模拟案例
- 新建项目
- 新建PropertiesAndArgsTest.java
- 编辑yml
- 测试运行
- 模拟案例(第二种形式)
- 直接注释yml文件
- 在springBootTest注解里声明
- 模拟案例(yml和注解都存在)
- 测试运行
- 模拟案例(args数组参数)
- 编辑PropertiesAndArgsTest
- 模拟案例(args,properties,yml整合)
- 测试结果
- 小结
新建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测试运行 模拟案例(第二种形式) 直接注释yml文件 在springBootTest注解里声明
模拟案例(yml和注解都存在)测试成功
测试运行yml
模拟案例(args数组参数)就近原则
编辑PropertiesAndArgsTestargs属性可以为当前测试用例添加临时的命令行参数
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
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注解后面,谁最后加载



