Back to Repositories

Testing Excel File Processing Workflows in EasyExcel

This test suite demonstrates Excel file reading capabilities using EasyExcel library in Java. It includes tests for reading local Excel files and remote Excel files through URLs, with various configuration options for header rows and data processing.

Test Coverage Overview

The test suite covers essential Excel reading operations with EasyExcel.

Key functionality includes:
  • Local Excel file reading with custom header configurations
  • Remote Excel file reading via URL
  • Synchronous and asynchronous reading operations
  • Custom listener implementation for data processing

Implementation Analysis

The testing approach utilizes JUnit Jupiter for test execution and validation. The implementation demonstrates both synchronous (doReadSync) and asynchronous (doRead) reading patterns, with configurable header row numbers and custom listener integration. EasyExcel’s fluent API is leveraged for readable and maintainable test cases.

Technical Details

Testing tools and configurations:
  • JUnit Jupiter test framework
  • EasyExcel library for Excel processing
  • SLF4J for logging
  • FastJSON for data serialization
  • Custom HgListener for data processing

Best Practices Demonstrated

The test suite exhibits several testing best practices including proper resource handling, separate test methods for different scenarios, and appropriate logging implementation. Each test method focuses on a specific functionality, making the suite maintainable and purpose-driven. The code demonstrates proper exception handling and stream management for file operations.

alibaba/easyexcel

easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/simple/HgTest.java

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

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.List;

import com.alibaba.excel.EasyExcel;
import com.alibaba.fastjson2.JSON;

import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 测试poi
 *
 * @author Jiaju Zhuang
 **/

public class HgTest {
    private static final Logger LOGGER = LoggerFactory.getLogger(HgTest.class);

    @Test
    public void hh() throws IOException {
        List<Object> list =
            EasyExcel.read(new FileInputStream("D:\\test\\201909301017rule.xlsx")).headRowNumber(2).sheet()
                .doReadSync();
        for (Object data : list) {
            LOGGER.info("返回数据:{}", JSON.toJSONString(data));
        }
    }

    @Test
    public void hh5() throws IOException {
        URL url = new URL(
            "http://hotelcontractfil.oss-cn-beijing.aliyuncs"
                + ".com/2019/%E5%98%89%E6%83%A0-%E4%B8%AD%E4%BA%A4%E5%BB%BA_2019-09-01_2019-09-30_1569055677522"
                + ".xlsx?Expires=1884415681&OSSAccessKeyId=LTAIGZDkqZfPArBr&Signature=Rf0gbO8vl3l%2Brj1KdyzHHMsUhCE"
                + "%3D");
        InputStream is = url.openStream();
        List<Object> list =
            EasyExcel.read(is).headRowNumber(0).sheet().doReadSync();
        for (Object data : list) {
            LOGGER.info("返回数据:{}", JSON.toJSONString(data));
        }
    }

    @Test
    public void hh2() throws IOException {
        EasyExcel.read(new FileInputStream("D:\\test\\商户不匹配工单信息收集表格.xlsx")).registerReadListener(
                new HgListener())
            .headRowNumber(0).sheet().doRead();
    }

}