Back to Repositories

Testing Exception Handling Mechanisms in EasyExcel

This test suite validates exception handling and data processing in EasyExcel across different file formats (XLSX, XLS, CSV). It verifies both normal operations and error scenarios when reading and writing Excel data.

Test Coverage Overview

The test suite provides comprehensive coverage of exception handling scenarios in EasyExcel.

  • Tests multiple file formats: XLSX (.07), XLS (.03), and CSV
  • Validates standard read/write operations
  • Tests exception throwing and handling
  • Verifies sheet-level exception management
  • Includes data validation across multiple sheets

Implementation Analysis

The testing approach utilizes JUnit Jupiter for structured test execution with methodical naming conventions.

Key patterns include:
  • BeforeAll setup for file initialization
  • Systematic test method ordering
  • Separate test cases for each file format
  • Custom exception listeners for different scenarios

Technical Details

Testing infrastructure includes:
  • JUnit Jupiter test framework
  • AssertJ for assertions
  • Custom FileUtil for test file management
  • EasyExcel API for Excel operations
  • Custom DataListener implementations

Best Practices Demonstrated

The test suite exemplifies several testing best practices:

  • Isolated test methods for different file formats
  • Proper resource handling with try-with-resources
  • Systematic test data generation
  • Thorough assertion checking
  • Clear separation of test scenarios

alibaba/easyexcel

easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/exception/ExceptionDataTest.java

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

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.alibaba.easyexcel.test.demo.write.DemoData;
import com.alibaba.easyexcel.test.util.TestFileUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;

import org.assertj.core.util.Lists;
import org.junit.jupiter.api.Assertions;
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 ExceptionDataTest {

    private static File file07;
    private static File file03;
    private static File fileCsv;
    private static File fileExcelAnalysisStopSheetException07;
    private static File fileExcelAnalysisStopSheetException03;
    private static File fileExcelAnalysisStopSheetExceptionCsv;
    private static File fileException07;
    private static File fileException03;

    @BeforeAll
    public static void init() {
        file07 = TestFileUtil.createNewFile("exception.xlsx");
        file03 = TestFileUtil.createNewFile("exception.xls");
        fileCsv = TestFileUtil.createNewFile("exception.csv");
        fileExcelAnalysisStopSheetException07 = TestFileUtil.createNewFile("excelAnalysisStopSheetException.xlsx");
        fileExcelAnalysisStopSheetException03 = TestFileUtil.createNewFile("excelAnalysisStopSheetException.xls");
        fileException07 = TestFileUtil.createNewFile("exceptionThrow.xlsx");
        fileException03 = TestFileUtil.createNewFile("exceptionThrow.xls");
    }

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

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

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

    @Test
    public void t11ReadAndWrite07() throws Exception {
        readAndWriteException(fileException07);
    }

    @Test
    public void t12ReadAndWrite03() throws Exception {
        readAndWriteException(fileException03);
    }

    @Test
    public void t21ReadAndWrite07() throws Exception {
        readAndWriteExcelAnalysisStopSheetException(fileExcelAnalysisStopSheetException07);
    }

    @Test
    public void t22ReadAndWrite03() throws Exception {
        readAndWriteExcelAnalysisStopSheetException(fileExcelAnalysisStopSheetException03);
    }


    private void readAndWriteExcelAnalysisStopSheetException(File file) throws Exception {
        try (ExcelWriter excelWriter = EasyExcel.write(file, ExceptionData.class).build()) {
            for (int i = 0; i < 5; i++) {
                String sheetName = "sheet" + i;
                WriteSheet writeSheet = EasyExcel.writerSheet(i, sheetName).build();
                List<ExceptionData> data = data(sheetName);
                excelWriter.write(data, writeSheet);
            }
        }

        ExcelAnalysisStopSheetExceptionDataListener excelAnalysisStopSheetExceptionDataListener
            = new ExcelAnalysisStopSheetExceptionDataListener();
        EasyExcel.read(file, ExceptionData.class, excelAnalysisStopSheetExceptionDataListener).doReadAll();
        Map<Integer, List<String>> dataMap = excelAnalysisStopSheetExceptionDataListener.getDataMap();
        Assertions.assertEquals(5, dataMap.size());
        for (int i = 0; i < 5; i++) {
            List<String> sheetDataList = dataMap.get(i);
            Assertions.assertNotNull(sheetDataList);
            Assertions.assertEquals(5, sheetDataList.size());
            String sheetName = "sheet" + i;

            for (String sheetData : sheetDataList) {
                Assertions.assertTrue(sheetData.startsWith(sheetName));
            }
        }
    }

    private void readAndWriteException(File file) throws Exception {
        EasyExcel.write(new FileOutputStream(file), ExceptionData.class).sheet().doWrite(data());
        ArithmeticException exception = Assertions.assertThrows(ArithmeticException.class, () -> EasyExcel.read(
            new FileInputStream(file), ExceptionData.class,
            new ExceptionThrowDataListener()).sheet().doRead());
        Assertions.assertEquals("/ by zero", exception.getMessage());
    }

    private void readAndWrite(File file) throws Exception {
        EasyExcel.write(new FileOutputStream(file), ExceptionData.class).sheet().doWrite(data());
        EasyExcel.read(new FileInputStream(file), ExceptionData.class, new ExceptionDataListener()).sheet().doRead();
    }

    private List<ExceptionData> data() {
        List<ExceptionData> list = new ArrayList<ExceptionData>();
        for (int i = 0; i < 10; i++) {
            ExceptionData simpleData = new ExceptionData();
            simpleData.setName("姓名" + i);
            list.add(simpleData);
        }
        return list;
    }

    private List<ExceptionData> data(String prefix) {
        List<ExceptionData> list = Lists.newArrayList();
        for (int i = 0; i < 10; i++) {
            ExceptionData simpleData = new ExceptionData();
            simpleData.setName(prefix + "-姓名" + i);
            list.add(simpleData);
        }
        return list;
    }
}