Back to Repositories

Testing Headerless Excel Operations in alibaba/easyexcel

This test suite validates EasyExcel’s functionality for handling Excel files without headers across different formats (XLSX, XLS, CSV). It demonstrates the framework’s capability to read and write data when header rows are explicitly disabled.

Test Coverage Overview

The test suite provides comprehensive coverage for headerless data operations across multiple file formats.

Key areas tested include:
  • XLSX (Excel 2007+) file handling without headers
  • XLS (Excel 2003) file handling without headers
  • CSV file handling without headers
  • Data reading and writing consistency checks

Implementation Analysis

The testing approach utilizes JUnit Jupiter for structured test execution with method ordering. Each test case follows a consistent pattern of writing data without headers and subsequently reading it back for verification.

Technical implementation features:
  • @TestMethodOrder annotation for controlled test execution
  • Shared file resources initialized via @BeforeAll
  • Parameterized test method for different file formats

Technical Details

Testing infrastructure includes:
  • EasyExcel core library for Excel operations
  • JUnit Jupiter test framework
  • Custom TestFileUtil for file management
  • NoHeadData class for data modeling
  • NoHeadDataListener for reading verification

Best Practices Demonstrated

The test suite exemplifies several testing best practices:

  • Clean separation of test setup and execution
  • Reusable test utilities
  • Consistent test method naming convention
  • Shared resource initialization
  • Parameterized test implementation for multiple formats

alibaba/easyexcel

easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/head/NoHeadDataTest.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 NoHeadDataTest {

    private static File file07;
    private static File file03;
    private static File fileCsv;

    @BeforeAll
    public static void init() {
        file07 = TestFileUtil.createNewFile("noHead07.xlsx");
        file03 = TestFileUtil.createNewFile("noHead03.xls");
        fileCsv = TestFileUtil.createNewFile("noHeadCsv.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, NoHeadData.class).needHead(Boolean.FALSE).sheet().doWrite(data());
        EasyExcel.read(file, NoHeadData.class, new NoHeadDataListener()).headRowNumber(0).sheet().doRead();
    }

    private List<NoHeadData> data() {
        List<NoHeadData> list = new ArrayList<NoHeadData>();
        NoHeadData data = new NoHeadData();
        data.setString("字符串0");
        list.add(data);
        return list;
    }
}