栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

SpringBoot开发实用篇 - 测试 -加载测试专用属性

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SpringBoot开发实用篇 - 测试 -加载测试专用属性

文章目录
  • 测试
  • 加载测试专用属性
    • 模拟案例
      • 新建项目
      • 新建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数组参数)

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

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注解后面,谁最后加载

小结

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/922049.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号