You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Java中使用JUnit测试SeparateOfficers方法及测试集设计

针对SeparateOfficers()方法的最小测试集设计

测试集设计思路

最小测试集需覆盖核心功能场景与边界异常,确保SeparateOfficers()方法能正确按年份分类记录、过滤无效数据并统计数量。测试记录存入testOfficers.txt,输出文件对应为testOfficers21.txt、testOfficers22.txt、testOfficers23.txt。

测试方法示例(内嵌测试集)

import org.junit.Test;
import java.io.IOException;

public class SeparateOfficersTest {

    @Test
    public void testSeparateOfficers() throws IOException {
        /* testOfficers.txt 完整内容:
        Smith, John, 2021, Finance
        Doe, Jane, 2022, HR
        Brown, Mike, 2023, Operations
        Wilson, Alice, 2020, Marketing
        , , , 
        Taylor, Bob, invalid_year, IT
        Lee, Sarah, 2021, Finance
        */
        
        // 调用待测试方法,指定输入输出路径
        SeparateOfficers.separate("testOfficers.txt", 
                                  "testOfficers21.txt", 
                                  "testOfficers22.txt", 
                                  "testOfficers23.txt");
        
        // 若后续可实现完整JUnit断言,可补充以下验证逻辑:
        // 1. 校验testOfficers21.txt含2条记录(Smith、Lee),统计数为2
        // 2. 校验testOfficers22.txt含1条记录(Doe),统计数为1
        // 3. 校验testOfficers23.txt含1条记录(Brown),统计数为1
        // 4. 校验2020年、无效格式、空行记录未被写入任何输出文件
    }
}

测试集覆盖场景

  • 核心分类验证:包含2021、2022、2023年的有效记录,其中2021年含多条数据,验证批量分类能力
  • 非目标年份过滤:加入2020年记录,验证不在指定范围内的数据会被排除
  • 异常格式处理:包含空行、年份字段无效的记录,验证此类数据不会被统计或写入
  • 多字段兼容性:不同部门的跨年份记录,验证分类逻辑不受非年份字段干扰

内容的提问来源于stack exchange,提问作者yahya zeyad

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.28 00:12:39