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

时间:2023-03-08 19:59:50

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

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

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

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

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

 package com.testdata;

 import org.springframework.boot.context.properties.ConfigurationProperties;

 /**
* Created by sallyzhang on 4/26/16.
*/ @ConfigurationProperties(prefix="home", locations = "classpath:homeTestData.properties")
public class HomeTestDataSettings { private String homeValue;
private String contentValue;
private String age;
private String builtYear;
private String constructionType;
private String roofType;
private String suburbStatePostcode;
private String address;
private String homeExcess;
private String contentExcess;
private String sourceOfBusiness;
private String username;
private String password; public String getHomeValue() {
return homeValue;
} public void setHomeValue(String homeValue) {
this.homeValue = homeValue;
} public String getContentValue() {
return contentValue;
} public void setContentValue(String contentValue) {
this.contentValue = contentValue;
} public String getAge() {
return age;
} public void setAge(String age) {
this.age = age;
} public String getBuiltYear() {
return builtYear;
} public void setBuiltYear(String builtYear) {
this.builtYear = builtYear;
} public String getConstructionType() {
return constructionType;
} public void setConstructionType(String constructionType) {
this.constructionType = constructionType;
} public String getRoofType() {
return roofType;
} public void setRoofType(String roofType) {
this.roofType = roofType;
} public String getSuburbStatePostcode() {
return suburbStatePostcode;
} public void setSuburbStatePostcode(String suburbStatePostcode) {
this.suburbStatePostcode = suburbStatePostcode;
} public String getAddress() {
return address;
} public void setAddress(String address) {
this.address = address;
} public String getHomeExcess() {
return homeExcess;
} public void setHomeExcess(String homeExcess) {
this.homeExcess = homeExcess;
} public String getContentExcess() {
return contentExcess;
} public void setContentExcess(String contentExcess) {
this.contentExcess = contentExcess;
} public String getSourceOfBusiness() {
return sourceOfBusiness;
} public void setSourceOfBusiness(String sourceOfBusiness) {
this.sourceOfBusiness = sourceOfBusiness;
} public String getUsername() {
return username;
} public void setUsername(String username) {
this.username = username;
} public String getPassword() {
return password;
} public void setPassword(String password) {
this.password = password;
}
}

HomeTestDataSettings

然后在入口类PropertyTestDataDemo加上EnableConfigurationProperties

 @SpringBootApplication
@EnableConfigurationProperties({HomeTestDataSettings.class,MotorTestDataSettings.class})
public class PropertyTestDataDemo { public static void main(String[] args) {
SpringApplication.run(PropertyTestDataDemo.class, args);
}
}

PropertyTestDataDemo

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

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