【发布时间】:2021-11-22 12:40:41
【问题描述】:
我创建了一个小应用程序,它在启动时使用 init 方法中的 openCsv 加载 excel 文件。该应用程序从我在 application.properties 中提供的路径加载我的 excel。
现在我想为此做一些测试。我该如何测试这种情况?我想在每个测试中创建一个 excel 文件,但使用不同的值,看看它是否正确加载了我的 excel。
Service
public class ReaderService {
private List<Person> personDetailsList;
@Value("${file.path}")
String filePath;
@PostConstruct
public void init() throws IOException {
personDetailsList = new CsvToBeanBuilder<Person>(new FileReader(filePath))
.withType(Person.class).build().parse();
}
public StatisticReport calculateStatistics() {
Integer firstNameNullCounter = 0;
Integer lastNameNullCounter = 0;
Integer ageNullCounter= 0;
for(Person person : personDetailsList){
if(person.getFirstName().equals("") || person.getFirstName() == null) {
firstNameNullCounter++;
}
if(person.getLastName().equals("") || person.getLastName() == null) {
lastNameNullCounter++;
}
if(person.getAge() == null){
ageNullCounter++;
}
}
return new StatisticReport(firstNameNullCounter,lastNameNullCounter,ageNullCounter,calculateAverage());
}
public double calculateAverage(){
return personDetailsList.stream()
.map(p -> p.getAge() == null ? new Person(p.getFirstName(),p.getLastName(),0) : p)
.collect(Collectors.averagingDouble(Person::getAge));
}
application.properties:
file.path=...some path...\data.csv
【问题讨论】: