• SpringBoot加载测试类属性和配置说明


    一、项目准备

    1.创建项目

    2.配置yml文件

    1. test:
    2. name: FOREVER
    3. love: sing

     二、测试类属性

    1.@Value

    说明:读取yml中的数据。

    1. package com.forever;
    2. import org.junit.jupiter.api.Test;
    3. import org.springframework.beans.factory.annotation.Value;
    4. import org.springframework.boot.test.context.SpringBootTest;
    5. @SpringBootTest
    6. class Springboot10TestApplicationTests {
    7. @Value("${test.name}")
    8. private String name;
    9. @Test
    10. void contextLoads() {
    11. System.out.println(name);
    12. }
    13. }

    2.@SpringBootTest

    说明:@SpringBootTest(properties = "test.name=Good");单文件临时生效。@SpringBootTest(args = {"--test.name=Better"})

    1. package com.forever;
    2. import org.junit.jupiter.api.Test;
    3. import org.springframework.beans.factory.annotation.Value;
    4. import org.springframework.boot.test.context.SpringBootTest;
    5. @SpringBootTest(properties = "test.name=Good")
    6. class Springboot10TestApplicationTests {
    7. @Value("${test.name}")
    8. private String name;
    9. @Test
    10. void contextLoads() {
    11. System.out.println(name);
    12. }
    13. }

     

     

    1. package com.forever;
    2. import org.junit.jupiter.api.Test;
    3. import org.springframework.beans.factory.annotation.Value;
    4. import org.springframework.boot.test.context.SpringBootTest;
    5. @SpringBootTest(args = {"--test.name=Better"})
    6. class Springboot10TestApplicationTests {
    7. @Value("${test.name}")
    8. private String name;
    9. @Test
    10. void contextLoads() {
    11. System.out.println(name);
    12. }
    13. }
     
    
    @SpringBootTest(properties = {"test.name=Good"},args = {"--test.name=Better"})

    说明:命令行的优先级高于其他的;args覆盖properties。

     三、测试类配置

    1.@Import

    说明:导入配置类,@Import(MsgConfig.class)。

    1.1创建配置类

    说明:开发中,不能将方法通过@Bean变成bean类。

    1. package com.forever.config;
    2. import org.springframework.context.annotation.Bean;
    3. import org.springframework.context.annotation.Configuration;
    4. @Configuration
    5. public class MsgConfig {
    6. //开发误用
    7. @Bean
    8. public String msg(){
    9. return "bean msg";
    10. }
    11. }

    1.2测试类

    说明:通过@Import追加配置。

    1. package com.forever;
    2. import com.forever.config.MsgConfig;
    3. import org.junit.jupiter.api.Test;
    4. import org.springframework.beans.factory.annotation.Autowired;
    5. import org.springframework.beans.factory.annotation.Value;
    6. import org.springframework.boot.test.context.SpringBootTest;
    7. import org.springframework.context.annotation.Import;
    8. @SpringBootTest
    9. //追加了配置
    10. @Import(MsgConfig.class)
    11. class Springboot10TestApplicationTests {
    12. @Autowired
    13. private String msg;
    14. @Test
    15. void contextLoads() {
    16. System.out.println(msg);
    17. }
    18. }

  • 相关阅读:
    Makefile(make)之(3)输出变量值
    重磅!这本SSCI期刊已解除On Hold状态!警惕目前6本SCIE/ESCI期刊被标记!
    【C# Programming】继承、接口
    利用 Amazon CodeWhisperer 激发孩子的编程兴趣
    Java项目源码javaweb花店销售管理系统
    引用——权限问题
    JavaScript运算符(运算符(算数运算符,递增和递减运算符,比较运算符,逻辑运算符,赋值运算符)、运算符优先级)
    基于Struts2+Hibernate开发学生信息管理系统(选课)
    变焦镜头内参数如何获得?
    【Java】中的String、StringBuffer和StringBuilder的区别
  • 原文地址:https://blog.csdn.net/m0_62785037/article/details/134299246