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

如何提升Salesforce Apex测试覆盖率至75%+并覆盖alerta变量

如何覆盖Salesforce Apex中alerta变量相关的代码分支并提升测试覆盖率

问题原因

当前测试仅创建了UniqueId_Status__c = 'M1'且UniqueId__c为null的Contact,只会触发alerta = false的分支,完全没有覆盖那些将alerta设为true的代码块,导致覆盖率不足。另外,测试未对方法返回值做断言,无法验证逻辑正确性。

解决方案

要覆盖所有分支,需要针对每个会将alerta设为true的条件创建测试数据,并断言方法返回值;同时还要覆盖其他边界情况(如空contactId、非智利的Contact等)。

改进后的测试类

@isTest public class ATCL_BloquearContactoTest {    
    // 测试分支1:UniqueId__c为null,Status属于M2/M3/M4/R2/R3/R4
    @IsTest static void testAlertaTrue_EmptyUniqueId_ValidStatus() {
        Contact testContact = new Contact(
            FirstName = 'Test1',
            LastName = 'Branch1',
            UniqueId_Status__c = 'M2', // 用目标分支中的一个状态
            Country__c = 'CHILE'
        );
        insert testContact;
        
        Boolean result = ATCL_BloquearContactoCTRL.getContacto(testContact.Id);
        System.assertEquals(true, result, '当UniqueId为空且状态为M2时,alerta应为true');
    }

    // 测试分支2:UniqueId__c不为null,Status属于M2/M3/M4/R2/R3/R4
    @IsTest static void testAlertaTrue_NonEmptyUniqueId_ValidStatus() {
        Contact testContact = new Contact(
            FirstName = 'Test2',
            LastName = 'Branch2',
            UniqueId__c = '12345',
            UniqueId_Status__c = 'R3',
            Country__c = 'CHILE'
        );
        insert testContact;
        
        Boolean result = ATCL_BloquearContactoCTRL.getContacto(testContact.Id);
        System.assertEquals(true, result, '当UniqueId不为空且状态为R3时,alerta应为true');
    }

    // 测试分支3:UniqueId__c不为null,Status不属于M2/M3/M4/R2/R3/R4且不是D1、不为null
    @IsTest static void testAlertaTrue_NonEmptyUniqueId_OtherValidStatus() {
        Contact testContact = new Contact(
            FirstName = 'Test3',
            LastName = 'Branch3',
            UniqueId__c = '67890',
            UniqueId_Status__c = 'M1', // 符合分支3的条件
            Country__c = 'CHILE'
        );
        insert testContact;
        
        Boolean result = ATCL_BloquearContactoCTRL.getContacto(testContact.Id);
        System.assertEquals(true, result, '当UniqueId不为空且状态为M1时,alerta应为true');
    }

    // 测试alerta为false的情况:Status为D1
    @IsTest static void testAlertaFalse_StatusD1() {
        Contact testContact = new Contact(
            FirstName = 'Test4',
            LastName = 'Branch4',
            UniqueId__c = '09876',
            UniqueId_Status__c = 'D1',
            Country__c = 'CHILE'
        );
        insert testContact;
        
        Boolean result = ATCL_BloquearContactoCTRL.getContacto(testContact.Id);
        System.assertEquals(false, result, '当状态为D1时,alerta应为false');
    }

    // 测试空contactId的情况
    @IsTest static void testAlertaFalse_EmptyContactId() {
        Boolean result = ATCL_BloquearContactoCTRL.getContacto('');
        System.assertEquals(false, result, '当contactId为空时,alerta应为false');
    }

    // 测试非智利Contact的情况(会触发查询异常,需捕获)
    @IsTest static void testQueryException_NonChileContact() {
        Contact testContact = new Contact(
            FirstName = 'Test5',
            LastName = 'Branch5',
            Country__c = 'ARGENTINA'
        );
        insert testContact;
        
        Test.startTest();
        try {
            ATCL_BloquearContactoCTRL.getContacto(testContact.Id);
            System.assert(false, '查询非智利Contact时应抛出异常');
        } catch (QueryException e) {
            System.assert(true, '捕获到预期的QueryException');
        }
        Test.stopTest();
    }
}

关键说明

  1. 无需直接访问alerta变量:方法的返回值就是alerta的值,通过断言返回结果即可验证对应代码分支是否被执行。
  2. 覆盖所有逻辑分支:每个测试方法对应一个代码分支,确保所有条件都被测试到,这样覆盖率会显著提升至75%以上。
  3. 添加断言:断言是测试的核心,不仅能验证逻辑正确性,还能确保代码分支确实被覆盖。

内容的提问来源于stack exchange,提问作者juan ventura rivas

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.18 04:50:10