+ Start a Discussion
subodh chaturvedi 17subodh chaturvedi 17 

how to write the test class of the below code

Below class updating the contact field (Account_status_BG__c)from the account field (Membership_Defined__c).
It is only covering 60 %. it is not going under if condition.
public class AccountStatusUpdate {
    
    public static void acntStatusMethod(List<Account> ac,Map<Id,Account> oldAccountMap){
        set<Id> accIds = new set<Id>();
        for(Account acc: ac){
            accIds.add(acc.Id);
        }
        
        if(accIds.size() > 0){
            List<Contact> contactList = [SELECT Id,Account_Status__c, AccountId, Account.Membership_Defined__c FROM Contact WHERE AccountId IN : accIds];
            //Create a list to update the contacts    
            List<Contact> contactToBeUpdate = new List<Contact>();
            for(Contact objCont : contactList){ 
                if(oldAccountMap!= null && String.isNotBlank(objCont.Account.Membership_Defined__c) && objCont.Account.Membership_Defined__c != oldAccountMap.get(objCont.AccountId).Membership_Defined__c){
                    Contact newContact = new Contact();
                    newContact.Id = objCont.Id;
                    newContact.Account_Status_BG__c = objCont.Account.Membership_Defined__c;
                    contactToBeUpdate.add(newContact);
                }
            }
            if(contactToBeUpdate.size() > 0){
                UPDATE contactToBeUpdate;
            }
        }
        
    }
    
}
Best Answer chosen by subodh chaturvedi 17
Raj VakatiRaj Vakati
Try this code
 
@isTest
private class AccountStatusUpdateTest {
    
    static testMethod void testCC () {
        
        
        Profile pf= [Select Id from profile where Name='System Administrator']; 
        
        String orgId=UserInfo.getOrganizationId(); 
        String dateString=String.valueof(Datetime.now()).replace(' ','').replace(':','').replace('-','') ;
        Integer RandomId=Integer.valueOf(Math.rint(Math.random()*1000000)); 
        String uniqueName=orgId+dateString+RandomId; 
        User uu=new User(firstname = 'ABC', 
                         lastName = 'XYZ', 
                         email = uniqueName + '@test' + orgId + '.org', 
                         Username = uniqueName + '@test' + orgId + '.org', 
                         EmailEncodingKey = 'ISO-8859-1', 
                         Alias = uniqueName.substring(18, 23), 
                         TimeZoneSidKey = 'America/Los_Angeles', 
                         LocaleSidKey = 'en_US', 
                         LanguageLocaleKey = 'en_US', 
                         ProfileId = pf.Id
                        ); 
        
        
        insert uu;
        System.runAs(uu){
            
            Account acc = new Account(Name='Test Account');
            acc.Membership_Defined__c ='new';
            insert acc;
            
             Account acc1 = new Account(Name='Test Account');
            acc1.Membership_Defined__c ='update';
            insert acc1;
            
            Contact cont1 = new Contact(LastName='ContTest1', Email='test1contact@duptest.com', AccountId = acc.Id, LeadSource
                                        ='Inbound Call');
            con1.Account_Status__c ='open';
            
            
            insert cont1;
            cont1.AccountId = acc1.id ; 
            update cont1;
            
        }
        
    }
    
}

 

All Answers

Raj VakatiRaj Vakati
Try this code
 
@isTest
private class AccountStatusUpdateTest {
    
    static testMethod void testCC () {
        
        
        Profile pf= [Select Id from profile where Name='System Administrator']; 
        
        String orgId=UserInfo.getOrganizationId(); 
        String dateString=String.valueof(Datetime.now()).replace(' ','').replace(':','').replace('-','') ;
        Integer RandomId=Integer.valueOf(Math.rint(Math.random()*1000000)); 
        String uniqueName=orgId+dateString+RandomId; 
        User uu=new User(firstname = 'ABC', 
                         lastName = 'XYZ', 
                         email = uniqueName + '@test' + orgId + '.org', 
                         Username = uniqueName + '@test' + orgId + '.org', 
                         EmailEncodingKey = 'ISO-8859-1', 
                         Alias = uniqueName.substring(18, 23), 
                         TimeZoneSidKey = 'America/Los_Angeles', 
                         LocaleSidKey = 'en_US', 
                         LanguageLocaleKey = 'en_US', 
                         ProfileId = pf.Id
                        ); 
        
        
        insert uu;
        System.runAs(uu){
            
            Account acc = new Account(Name='Test Account');
            acc.Membership_Defined__c ='new';
            insert acc;
            
             Account acc1 = new Account(Name='Test Account');
            acc1.Membership_Defined__c ='update';
            insert acc1;
            
            Contact cont1 = new Contact(LastName='ContTest1', Email='test1contact@duptest.com', AccountId = acc.Id, LeadSource
                                        ='Inbound Call');
            con1.Account_Status__c ='open';
            
            
            insert cont1;
            cont1.AccountId = acc1.id ; 
            update cont1;
            
        }
        
    }
    
}

 
This was selected as the best answer
subodh chaturvedi 17subodh chaturvedi 17
Thanks, Raj for your response.