简体   繁体   中英

Salesforce Apex increase coverage test

My test coverage is up to 66%, how can I increase? I do not know how to acces or use the alerta variable into the test class, I want to increase the test covergae from 66% to atleast 75% or even more

Apex class:

public static Boolean getContacto (String contactId) {
    Boolean alerta = false;
    if(String.isNotBlank(contactId) && String.isNotEmpty(contactId)){
        Contact contacto =[SELECT Id, UniqueId_Status__c, UniqueId__c from Contact where Id =: contactId and Country__c = 'CHILE'];
        
        if(contacto.UniqueId__c == null && (contacto.UniqueId_Status__c == 'M2' || contacto.UniqueId_Status__c == 'M3' || contacto.UniqueId_Status__c == 'M4' || contacto.UniqueId_Status__c == 'R2' || contacto.UniqueId_Status__c == 'R3' || contacto.UniqueId_Status__c == 'R4')){
            alerta = true;
        }else
        if(contacto.UniqueId__c != null && (contacto.UniqueId_Status__c == 'M2' || contacto.UniqueId_Status__c == 'M3' || contacto.UniqueId_Status__c == 'M4' || contacto.UniqueId_Status__c == 'R2' || contacto.UniqueId_Status__c == 'R3' || contacto.UniqueId_Status__c == 'R4')){
            alerta = true;   
        }else
        if(contacto.UniqueId__c != null && contacto.UniqueId_Status__c != 'M2' && contacto.UniqueId_Status__c != 'M3' && contacto.UniqueId_Status__c != 'M4' && contacto.UniqueId_Status__c != 'R2' && contacto.UniqueId_Status__c != 'R3' && contacto.UniqueId_Status__c != 'R4' && contacto.UniqueId_Status__c != 'D1' && contacto.UniqueId_Status__c != null){
            alerta = true;   
        }
    }        
    return alerta;
}

TestClass:

@isTest public class ATCL_BloquearContactoTest {    @IsTest
    static void bloqueaContactoTest(){
        Contact contactos = new Contact();
        
        contactos.FirstName = 'Amanda';
        contactos.LastName = 'testing';
        contactos.UniqueId_Status__c = 'M1';
        contactos.Country__c = 'CHILE';
        insert contactos;
        
        Test.startTest();
        ATCL_BloquearContactoCTRL.getContacto(contactos.Id);
        Test.stopTest();
    } }

I do not Know how to coverage alerta variable

Thanks Juan

The low key is to reckon those ifs as diferent scenarios. To achive those diferente scenarios you must add the new values into the test class to eventually pass the ifs sentences. Every scenrio should be resolve with a difrent @istest method:

public class ATCL_BloquearContactoTest {
@IsTest
static void bloqueaContactoTestNull(){
    Contact contactos = new Contact();
    
    contactos.FirstName = 'Amanda';
    contactos.LastName = 'testing';
    contactos.UniqueId__c = null;//== null
    contactos.UniqueId_Status__c = 'M2';//like M_ || R_
    contactos.Country__c = 'CHILE';
    insert contactos;
    
    Test.startTest();
    ATCL_BloquearContactoCTRL.getContacto(contactos.Id);
    Test.stopTest();

}
@IsTest
static void bloqueaContactoTestNotNull(){
    Contact contactos = new Contact();
    
    contactos.FirstName = 'Amanda';
    contactos.LastName = 'testing';
    contactos.UniqueId__c = '21354689';//!= null
    contactos.UniqueId_Status__c = 'M2';//like M_ || R_
    contactos.Country__c = 'CHILE';
    insert contactos;
    
    Test.startTest();
    ATCL_BloquearContactoCTRL.getContacto(contactos.Id);
    Test.stopTest();

}
@IsTest
static void bloqueaContactoTestNotNullNotNull(){
    Contact contactos = new Contact();
    
    contactos.FirstName = 'Amanda';
    contactos.LastName = 'testing';
    contactos.UniqueId__c = '21354689';//!= null
    contactos.UniqueId_Status__c = '0';//!(like  M_ || R_)
    contactos.Country__c = 'CHILE';
    insert contactos;
    
    Test.startTest();
    ATCL_BloquearContactoCTRL.getContacto(contactos.Id);
    Test.stopTest();

}

}

Bye

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM