Back to Repositories

Testing Annotation-Based Data Mapping Implementation in EasyExcel

This test suite validates the functionality of EasyExcel’s annotation-based index and name handling capabilities across different file formats (XLSX, XLS, CSV). It demonstrates the framework’s ability to read and write data using annotated model classes with specific index mappings.

Test Coverage Overview

The test suite provides comprehensive coverage of EasyExcel’s annotation processing capabilities across multiple file formats.

  • Tests read/write operations for XLSX (.07), XLS (.03), and CSV formats
  • Validates index-based column mapping using annotations
  • Ensures consistent data handling across different file types
  • Verifies proper serialization and deserialization of annotated fields

Implementation Analysis

The testing approach employs JUnit Jupiter with a methodical structure for validating annotation functionality.

The implementation uses a shared readAndWrite method pattern for consistent testing across file formats, leveraging EasyExcel’s fluent API for both reading and writing operations. Test methods are ordered using @TestMethodOrder to ensure sequential execution.

Technical Details

  • JUnit Jupiter test framework with @TestMethodOrder annotation
  • EasyExcel API for Excel/CSV operations
  • Custom TestFileUtil for file management
  • Dedicated AnnotationIndexAndNameData model class
  • Custom AnnotationIndexAndNameDataListener for validation

Best Practices Demonstrated

The test suite exemplifies several testing best practices in Java.

  • Proper test initialization using @BeforeAll
  • DRY principle through shared test logic
  • Consistent test naming conventions
  • Separate data generation methods
  • Clear separation of concerns between test setup and execution

alibaba/easyexcel

easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/annotation/AnnotationIndexAndNameDataTest.java

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

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;

/**
 * Annotation data test
 *
 * @author Jiaju Zhuang
 */
@TestMethodOrder(MethodOrderer.MethodName.class)
public class AnnotationIndexAndNameDataTest {

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

    @BeforeAll
    public static void init() {
        file07 = TestFileUtil.createNewFile("annotationIndexAndName07.xlsx");
        file03 = TestFileUtil.createNewFile("annotationIndexAndName03.xls");
        fileCsv = TestFileUtil.createNewFile("annotationIndexAndNameCsv.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, AnnotationIndexAndNameData.class).sheet().doWrite(data());
        EasyExcel.read(file, AnnotationIndexAndNameData.class, new AnnotationIndexAndNameDataListener()).sheet()
            .doRead();
    }

    private List<AnnotationIndexAndNameData> data() {
        List<AnnotationIndexAndNameData> list = new ArrayList<AnnotationIndexAndNameData>();
        AnnotationIndexAndNameData data = new AnnotationIndexAndNameData();
        data.setIndex0("第0个");
        data.setIndex1("第1个");
        data.setIndex2("第2个");
        data.setIndex4("第4个");
        list.add(data);
        return list;
    }
}