Back to Repositories

Testing Excel Data Parsing and Number Conversion in EasyExcel

This test suite validates Excel data parsing functionality in EasyExcel, focusing on issue #2443 and number conversion capabilities. The tests examine Excel file reading with date formats and integer parsing from decimal strings.

Test Coverage Overview

The test suite provides comprehensive coverage of Excel data parsing scenarios:

  • Excel file reading with different date formats
  • Decimal string to integer conversion
  • Page-based data reading implementation
  • Number format parsing edge cases

Implementation Analysis

The testing approach utilizes JUnit Jupiter framework with PageReadListener for Excel processing. The implementation demonstrates proper separation of concerns between file reading and number parsing tests, with dedicated test methods for each scenario.

Technical patterns include:
  • File path handling with TestFileUtil
  • JSON data validation
  • Logging integration with Slf4j

Technical Details

Testing tools and configuration:

  • JUnit Jupiter for test execution
  • EasyExcel API for Excel operations
  • Fastjson2 for JSON processing
  • Lombok for logging annotations
  • TestFileUtil for test file management

Best Practices Demonstrated

The test suite exhibits several testing best practices:

  • Clear test method naming conventions
  • Isolated test scenarios for different functionalities
  • Proper assertion usage for validation
  • Structured error handling with ParseException
  • Documented issue references for traceability

alibaba/easyexcel

easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/issue2443/Issue2443Test.java

            
package com.alibaba.easyexcel.test.temp.issue2443;

import java.io.File;
import java.text.ParseException;

import com.alibaba.easyexcel.test.util.TestFileUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.metadata.property.ExcelContentProperty;
import com.alibaba.excel.read.listener.PageReadListener;
import com.alibaba.excel.util.NumberUtils;
import com.alibaba.fastjson2.JSON;

import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;


@Slf4j
public class Issue2443Test {
    //CS304 (manually written) Issue link: https://github.com/alibaba/easyexcel/issues/2443
    @Test
    public void IssueTest1() {
        String fileName = TestFileUtil.getPath() + "temp/issue2443" + File.separator + "date1.xlsx";
        EasyExcel.read(fileName, Issue2443.class, new PageReadListener<Issue2443>(dataList -> {
            for (Issue2443 issueData : dataList) {
                log.info("读取到一条数据{}", JSON.toJSONString(issueData));
            }
        })).sheet().doRead();
    }

    //CS304 (manually written) Issue link: https://github.com/alibaba/easyexcel/issues/2443
    @Test
    public void IssueTest2() {
        String fileName = TestFileUtil.getPath() + "temp/issue2443" + File.separator + "date2.xlsx";
        EasyExcel.read(fileName, Issue2443.class, new PageReadListener<Issue2443>(dataList -> {
            for (Issue2443 issueData : dataList) {
                log.info("读取到一条数据{}", JSON.toJSONString(issueData));
            }
        })).sheet().doRead();
    }

    @Test
    public void parseIntegerTest1() throws ParseException {
        String string = "1.00";
        ExcelContentProperty contentProperty = null;
        int Int = NumberUtils.parseInteger(string, contentProperty);
        Assertions.assertEquals(1, Int);
    }

    @Test
    public void parseIntegerTest2() throws ParseException {
        String string = "2.00";
        ExcelContentProperty contentProperty = null;
        int Int = NumberUtils.parseInteger(string, contentProperty);
        Assertions.assertEquals(2, Int);
    }

}