【问题标题】:How to unit test spring PostConstruct and change application properties如何对spring PostConstruct进行单元测试并更改应用程序属性
【发布时间】: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

【问题讨论】:

    标签: spring junit opencsv


    【解决方案1】:
    1. 使用构造函数注入使您的代码更测试友好
    @Service
    public class ReaderService {
    
        private List<Person> personDetailsList;
    
        public ReaderService(@Value("${file.path}") String csv) throws IOException {
            personDetailsList = new CsvToBeanBuilder<Person>(new FileReader(csv))
                    .withType(Person.class).build().parse();
        }
        ...
        
    
    1. 做一个Junit测试,你可以使用ReaderService作为一个基本的java类(不需要它是一个组件)
    // fast not-spring-aware junit test
    class ReaderServiceTest {
      
      @Test
      public void testOne(){
        StatisticReport report = new ReaderService("path_to_file_one").calculateStatistics();
       // actual result testing
      }
      
      @Test
      public void testTwo(){
        StatisticReport report = new ReaderService("path_to_file_two").calculateStatistics();
       // actual result testing
      }
    }
    
    

    【讨论】:

    • 有一种方法可以从我的测试文件中的 application.propperties 获取路径?
    • 您不想要多条路径吗?每个测试的路径?
    • 1 个相同的测试路径,我已经在 application.properties 中声明了
    • 所以你可以像value_of_base_path_for_tests + "/file_one.csv"一样使用它?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-11
    • 1970-01-01
    • 1970-01-01
    • 2019-01-24
    • 2016-04-04
    • 1970-01-01
    相关资源
    最近更新 更多