基于spring-boot的测试桩设计-添加配置文件(properties)

编写测试时,有些内容可以放到配置文件中。

第一步:新增配置文件 conf.properties

 

 第二步:编写配置文件类 MockConf

 1 package mock.mockdemo.conf;
 2 
 3 import lombok.Data;
 4 import org.springframework.beans.factory.annotation.Value;
 5 import org.springframework.context.annotation.PropertySource;
 6 import org.springframework.stereotype.Component;
 7 
 8 
 9 @Data
10 @Component
11 @PropertySource("classpath:config/conf.properties")
12 public class MockConf {
13     @Value("${user_num}")
14     public String userNum;
15 }

第三步:添加接口和实现类

接口:

实现类:

 

 

 第四步:编写controller

测试:

 

 结果:可以正常获取到配置文件内容

 源码地址:

https://github.com/yuesest/mockdemo

猜你喜欢

转载自www.cnblogs.com/moonpool/p/11521109.html