Back to Repositories

Testing Excel Template Operations in alibaba/easyexcel

This test suite validates Excel template functionality in EasyExcel, focusing on reading and writing operations for both .xlsx and .xls file formats. It demonstrates template-based data manipulation with custom headers and data structures.

Test Coverage Overview

The test suite provides comprehensive coverage of template-based Excel operations.

Key areas tested include:
  • Excel 2007 (.xlsx) template reading and writing
  • Excel 2003 (.xls) template reading and writing
  • Custom header row positioning
  • Template-based data population

Implementation Analysis

The testing approach utilizes JUnit Jupiter for structured test execution with ordered test methods. Implementation employs EasyExcel’s fluent API for template operations, demonstrating both file format handlers and data binding capabilities.

Notable patterns include:
  • Template file initialization in @BeforeAll setup
  • Separate test cases for different Excel formats
  • Reusable data generation methods

Technical Details

Testing infrastructure includes:
  • JUnit Jupiter test framework
  • EasyExcel API for Excel operations
  • Custom TestFileUtil for file handling
  • TemplateDataListener for data validation
  • File format support: .xlsx and .xls

Best Practices Demonstrated

The test suite exemplifies several testing best practices:

  • Clear test method organization with @TestMethodOrder
  • Proper test setup and resource management
  • Separation of concerns between test data and test logic
  • Consistent error handling and resource cleanup
  • Reusable helper methods for common operations

alibaba/easyexcel

easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/template/TemplateDataTest.java

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

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 TemplateDataTest {

    private static File file07;
    private static File file03;

    @BeforeAll
    public static void init() {
        file07 = TestFileUtil.createNewFile("template07.xlsx");
        file03 = TestFileUtil.createNewFile("template03.xls");
    }

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

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

    private void readAndWrite07(File file) {
        EasyExcel.write(file, TemplateData.class)
            .withTemplate(TestFileUtil.readFile("template" + File.separator + "template07.xlsx")).sheet()
            .doWrite(data());
        EasyExcel.read(file, TemplateData.class, new TemplateDataListener()).headRowNumber(3).sheet().doRead();
    }

    private void readAndWrite03(File file) {
        EasyExcel.write(file, TemplateData.class)
            .withTemplate(TestFileUtil.readFile("template" + File.separator + "template03.xls")).sheet()
            .doWrite(data());
        EasyExcel.read(file, TemplateData.class, new TemplateDataListener()).headRowNumber(3).sheet().doRead();
    }

    private List<TemplateData> data() {
        List<TemplateData> list = new ArrayList<TemplateData>();
        TemplateData data = new TemplateData();
        data.setString0("字符串0");
        data.setString1("字符串01");
        TemplateData data1 = new TemplateData();
        data1.setString0("字符串1");
        data1.setString1("字符串11");
        list.add(data);
        list.add(data1);
        return list;
    }
}