junit测试类获取springboot的yml配置文件参数

1.类上增加注解

@RunWith(SpringRunner.class)
@SpringBootTest()

2.获取配置参数值注解

    @Value("${pay.mid}")
    private String mid;

测试效果

  • application.yml文件
pay:
  mid: 123456
  •  junit测试类
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest()
public class DemoJunitTest {
    @Value("${pay.mid}")
    private String mid;

    @Test
    public void getPayMid() {
        System.out.println("mid->"+mid);
    }
}
  • 运行结果 

 3.获取指定配置文件的参数,例如获取application-dev.yml

  • 测试类多增加个注解
@ActiveProfiles("dev")
  •  application-dev.yml配置文件内容
pay:
  mid: 654321
  •  junit测试类
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest()
@ActiveProfiles("dev")
public class DemoJunitTest {
    @Value("${pay.mid}")
    private String mid;

    @Test
    public void getPayMid() {
        System.out.println("mid->"+mid);
    }
}
  • 运行结果 

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
回答: 在Spring Boot中配置Redis需要进行以下几个步骤。首先,在Pom.xml文件中添加Redis的依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> ``` 接下来,在测试中添加Redis的相关配置信息和注解: ```java import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.test.context.junit4.SpringRunner; import java.util.concurrent.TimeUnit; @RunWith(SpringRunner.class) @SpringBootTest public class TestClass { @Autowired private RedisTemplate redisTemplate; @Test public void A() throws InterruptedException { redisTemplate.delete("key1"); } } ``` 然后,在yml文件中进行Redis的配置,注意Redis是Spring的直接下级目录。可以按照以下格式配置相关参数: ```yaml redis: database: 0 host: 127.0.0.1 port: 6379 password: jedis: pool: max-active: 8 max-wait: -1 max-idle: 8 min-idle: 0 timeout: 10000 ``` 这样就完成了Spring Boot中Redis的配置。通过以上步骤,你可以在Spring Boot中使用Redis来存储和获取数据了。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [【突破舒适圈】在SpringBoot中配置Redis](https://blog.csdn.net/qq_41997592/article/details/122866809)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值