Back to Repositories

Testing Complex Header Management in EasyExcel

This test suite validates complex header handling in EasyExcel, focusing on reading and writing Excel files with merged headers across different file formats (XLSX, XLS, CSV). It demonstrates both automatic and manual header merging capabilities.

Test Coverage Overview

The test suite provides comprehensive coverage for complex header operations in EasyExcel.

Key areas tested include:
  • File format compatibility (XLSX, XLS, CSV)
  • Automatic header merging functionality
  • Custom header configuration
  • Data reading and writing operations
  • SAX parser implementation for XLSX files

Implementation Analysis

The testing approach employs JUnit Jupiter for structured test execution with method ordering. Each test case follows a consistent pattern of file creation, data writing, and subsequent validation through reading.

Technical implementation features:
  • BeforeAll setup for test file initialization
  • Parameterized testing across file formats
  • Dedicated test methods for automatic header merging
  • Custom SAX parser configuration for XLSX processing

Technical Details

Testing infrastructure includes:
  • JUnit Jupiter test framework
  • EasyExcel core library
  • Custom TestFileUtil for file management
  • ComplexDataListener for data validation
  • SAXParserFactory implementation for XLSX processing
  • Support for multiple Excel formats (07, 03) and CSV

Best Practices Demonstrated

The test suite exemplifies several testing best practices in Java.

Notable implementations include:
  • Proper test initialization and resource management
  • Consistent method naming and organization
  • Reusable test utility methods
  • Separation of test data generation
  • Format-specific test cases
  • Clear test method organization using TestMethodOrder

alibaba/easyexcel

easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/head/ComplexHeadDataTest.java

            
package com.alibaba.easyexcel.test.core.head;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import com.alibaba.easyexcel.test.util.TestFileUtil;
import com.alibaba.excel.EasyExcel;

import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.MethodOrderer;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder;

/**
 * @author Jiaju Zhuang
 */
@TestMethodOrder(MethodOrderer.MethodName.class)
public class ComplexHeadDataTest {

    private static File file07;
    private static File file03;
    private static File fileCsv;
    private static File file07AutomaticMergeHead;
    private static File file03AutomaticMergeHead;
    private static File fileCsvAutomaticMergeHead;

    @BeforeAll
    public static void init() {
        file07 = TestFileUtil.createNewFile("complexHead07.xlsx");
        file03 = TestFileUtil.createNewFile("complexHead03.xls");
        fileCsv = TestFileUtil.createNewFile("complexHeadCsv.csv");
        file07AutomaticMergeHead = TestFileUtil.createNewFile("complexHeadAutomaticMergeHead07.xlsx");
        file03AutomaticMergeHead = TestFileUtil.createNewFile("complexHeadAutomaticMergeHead03.xls");
        fileCsvAutomaticMergeHead = TestFileUtil.createNewFile("complexHeadAutomaticMergeHeadCsv.csv");
    }

    @Test
    public void t01ReadAndWrite07() {
        readAndWrite(file07);
    }

    @Test
    public void t02ReadAndWrite03() {
        readAndWrite(file03);
    }

    @Test
    public void t03ReadAndWriteCsv() {
        readAndWrite(fileCsv);
    }

    private void readAndWrite(File file) {
        EasyExcel.write(file, ComplexHeadData.class).sheet().doWrite(data());
        EasyExcel.read(file, ComplexHeadData.class, new ComplexDataListener())
            .xlsxSAXParserFactoryName("com.sun.org.apache.xerces.internal.jaxp.SAXParserFactoryImpl").sheet().doRead();
    }

    @Test
    public void t11ReadAndWriteAutomaticMergeHead07() {
        readAndWriteAutomaticMergeHead(file07AutomaticMergeHead);
    }

    @Test
    public void t12ReadAndWriteAutomaticMergeHead03() {
        readAndWriteAutomaticMergeHead(file03AutomaticMergeHead);
    }

    @Test
    public void t13ReadAndWriteAutomaticMergeHeadCsv() {
        readAndWriteAutomaticMergeHead(fileCsvAutomaticMergeHead);
    }

    private void readAndWriteAutomaticMergeHead(File file) {
        EasyExcel.write(file, ComplexHeadData.class).automaticMergeHead(Boolean.FALSE).sheet().doWrite(data());
        EasyExcel.read(file, ComplexHeadData.class, new ComplexDataListener()).sheet().doRead();
    }

    private List<ComplexHeadData> data() {
        List<ComplexHeadData> list = new ArrayList<ComplexHeadData>();
        ComplexHeadData data = new ComplexHeadData();
        data.setString0("字符串0");
        data.setString1("字符串1");
        data.setString2("字符串2");
        data.setString3("字符串3");
        data.setString4("字符串4");
        list.add(data);
        return list;
    }
}