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

如何为SearchByusername方法编写正确的JUnit测试用例

第一步:先修复SearchByusername方法的逻辑错误

你当前的业务方法本身存在逻辑漏洞:循环中只要后续元素不匹配目标用户名,就会把之前匹配到的索引覆盖为-1,所以只有当目标用户是列表最后一个元素时才能返回正确索引,其余情况都会返回-1,这是你测试不通过的核心原因。

修复后的代码参考:

public static int SearchByusername(ArrayList<Account> A, String username) {
    int searchListLength = A.size();
    for (int i = 0; i < searchListLength; i++) {
        if (A.get(i).getUsername().equalsIgnoreCase(username)) {
            // 匹配到直接返回索引,不需要继续遍历
            return i;
        }
    }
    // 遍历完所有元素都没匹配到,返回-1
    return -1;
}

如果你需要保留exists变量的写法,去掉else分支的赋值即可:

public static int SearchByusername(ArrayList<Account> A, String username) {
    int exists = -1;
    int searchListLength = A.size();
    for (int i = 0; i < searchListLength; i++) {
        if (A.get(i).getUsername().equalsIgnoreCase(username)) {
            exists = i;
            // 可选:如果只需要第一个匹配的索引,加break提前结束循环
            break;
        }
    }
    return exists;
}

第二步:编写覆盖全场景的JUnit测试

建议覆盖以下测试场景,确保逻辑正确性:

  • 目标用户在列表首位
  • 目标用户在列表中间位
  • 目标用户在列表末位
  • 目标用户名不存在
  • 传入空列表
  • 用户名大小写不一致的匹配校验(因为方法用了equalsIgnoreCase)

以下是JUnit 5的示例代码:

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
import java.util.ArrayList;

public class SaudiGuideTest {
    private ArrayList<Account> accountINFO;

    // 每次测试前初始化测试数据,避免用例之间互相影响
    @BeforeEach
    void initTestData() {
        accountINFO = new ArrayList<>();
        Account user_1 = new Account("Sarah", "Naghi", "Sarh@hotmail.com", "0500000001", "Sarah", "Ss@12345678", 23);
        Account user_2 = new Account("Rawan", "Samer", "Rawan@hotmail.com", "0500000002", "RawanS", "Rr@12345678", 22);
        Account user_3 = new Account("Ragad", "Fahad", "Ragad@hotmail.com", "0500000003", "RagadFt", "Rr@12345678", 22);
        Account user_4 = new Account("Leen", "Saleh", "Leen@hotmail.com", "0500000004", "LeenS", "Ll@12345678", 23);
        accountINFO.add(user_1);
        accountINFO.add(user_2);
        accountINFO.add(user_3);
        accountINFO.add(user_4);
    }

    // 测试匹配首元素
    @Test
    void testSearchByusername_matchFirstElement() {
        int result = SaudiGuide.SearchByusername(accountINFO, "Sarah");
        assertEquals(0, result);
    }

    // 测试匹配中间元素
    @Test
    void testSearchByusername_matchMiddleElement() {
        int result = SaudiGuide.SearchByusername(accountINFO, "RagadFt");
        assertEquals(2, result);
    }

    // 测试匹配尾元素
    @Test
    void testSearchByusername_matchLastElement() {
        int result = SaudiGuide.SearchByusername(accountINFO, "LeenS");
        assertEquals(3, result);
    }

    // 测试用户名不存在
    @Test
    void testSearchByusername_notExist() {
        int result = SaudiGuide.SearchByusername(accountINFO, "Shaima");
        assertEquals(-1, result);
    }

    // 测试大小写不敏感匹配
    @Test
    void testSearchByusername_caseInsensitive() {
        int result = SaudiGuide.SearchByusername(accountINFO, "sarah");
        assertEquals(0, result);
    }

    // 测试传入空列表
    @Test
    void testSearchByusername_emptyList() {
        int result = SaudiGuide.SearchByusername(new ArrayList<>(), "Sarah");
        assertEquals(-1, result);
    }
}

如果使用JUnit 4,只需把注解换成@Before、@Test,导入包换成org.junit.Test、org.junit.Assert.assertEquals即可。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.24 19:06:04