使用Spring的Property文件存储测试数据 - 添加测试数据

测试数据直接写在Property文件中,如下图:

application.properties是系统自动生成,我添加了两个Property文件:HomeTestData.properties和motorTestData.properties来存储不同的测试数据。数据只要遵循Key=value格式即可,就像下面这样:

接下来为添加的两个Property文件中的数据生成get和set方法:

  1 package com.testdata;
  2 
  3 import org.springframework.boot.context.properties.ConfigurationProperties;
  4 
  5 /**
  6  * Created by sallyzhang on 4/26/16.
  7  */
  8 
  9 @ConfigurationProperties(prefix="home", locations = "classpath:homeTestData.properties")
 10 public class HomeTestDataSettings {
 11 
 12     private String homeValue;
 13     private String contentValue;
 14     private String age;
 15     private String builtYear;
 16     private String constructionType;
 17     private String roofType;
 18     private String suburbStatePostcode;
 19     private String address;
 20     private String homeExcess;
 21     private String contentExcess;
 22     private String sourceOfBusiness;
 23     private String username;
 24     private String password;
 25 
 26     public String getHomeValue() {
 27         return homeValue;
 28     }
 29 
 30     public void setHomeValue(String homeValue) {
 31         this.homeValue = homeValue;
 32     }
 33 
 34     public String getContentValue() {
 35         return contentValue;
 36     }
 37 
 38     public void setContentValue(String contentValue) {
 39         this.contentValue = contentValue;
 40     }
 41 
 42     public String getAge() {
 43         return age;
 44     }
 45 
 46     public void setAge(String age) {
 47         this.age = age;
 48     }
 49 
 50     public String getBuiltYear() {
 51         return builtYear;
 52     }
 53 
 54     public void setBuiltYear(String builtYear) {
 55         this.builtYear = builtYear;
 56     }
 57 
 58     public String getConstructionType() {
 59         return constructionType;
 60     }
 61 
 62     public void setConstructionType(String constructionType) {
 63         this.constructionType = constructionType;
 64     }
 65 
 66     public String getRoofType() {
 67         return roofType;
 68     }
 69 
 70     public void setRoofType(String roofType) {
 71         this.roofType = roofType;
 72     }
 73 
 74     public String getSuburbStatePostcode() {
 75         return suburbStatePostcode;
 76     }
 77 
 78     public void setSuburbStatePostcode(String suburbStatePostcode) {
 79         this.suburbStatePostcode = suburbStatePostcode;
 80     }
 81 
 82     public String getAddress() {
 83         return address;
 84     }
 85 
 86     public void setAddress(String address) {
 87         this.address = address;
 88     }
 89 
 90     public String getHomeExcess() {
 91         return homeExcess;
 92     }
 93 
 94     public void setHomeExcess(String homeExcess) {
 95         this.homeExcess = homeExcess;
 96     }
 97 
 98     public String getContentExcess() {
 99         return contentExcess;
100     }
101 
102     public void setContentExcess(String contentExcess) {
103         this.contentExcess = contentExcess;
104     }
105 
106     public String getSourceOfBusiness() {
107         return sourceOfBusiness;
108     }
109 
110     public void setSourceOfBusiness(String sourceOfBusiness) {
111         this.sourceOfBusiness = sourceOfBusiness;
112     }
113 
114     public String getUsername() {
115         return username;
116     }
117 
118     public void setUsername(String username) {
119         this.username = username;
120     }
121 
122     public String getPassword() {
123         return password;
124     }
125 
126     public void setPassword(String password) {
127         this.password = password;
128     }
129 }
HomeTestDataSettings

然后在入口类PropertyTestDataDemo加上EnableConfigurationProperties

1 @SpringBootApplication
2 @EnableConfigurationProperties({HomeTestDataSettings.class,MotorTestDataSettings.class})
3 public class PropertyTestDataDemo {
4 
5     public static void main(String[] args) {
6         SpringApplication.run(PropertyTestDataDemo.class, args);
7     }
8 }
PropertyTestDataDemo

这里解释下,PropertyTestDataDemo是SpringBoot的入口类,里面的mian函数就是入口,如果实在不明白,就记住:不要删了,就可以了。

如需转载,请注明出处,这是对他人劳动成果的尊重~

原文地址:https://www.cnblogs.com/sallyzhang/p/5458787.html