function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Sebastian PageSebastian Page 

Some line of apex class is not covered

Hello All

Some line in apex class is not covering please help me.

User-added image



 
public class UpdateAccountManagerEmailAdderss {
        
        public Static void updateAccountMgrEmail(List<case> lstcase)
        {   
         List<String> lstOfDomain=new  List<String>();  
    
         list <Account> lstAccounts = [Select Id,Account_Manager_Email__c from Account WHERE Old_HD_Provider__c!='ConnectWise' AND Status__c='Live'];
          Map<String,String>mapAccount=new  Map<String,String>();
            
            for(account a:lstAccounts){              mapAccount.put(a.id,a.Account_Manager_Email__c); 
      		}
    Map<string,string> mapDomain=new Map<String,String>();
    for(Account ks:[Select id,institutional_EmailDomain__c from Account WHERE Old_HD_Provider__c!='ConnectWise' AND Status__c='Live' and institutional_EmailDomain__c!=null])
    {
        lstOfDomain.add(ks.institutional_EmailDomain__c);
        mapDomain.put(ks.institutional_EmailDomain__c, ks.id);                                                      
                                                                   
        }
       for(case cs:lstcase)
         {
        if(lstOfDomain.size()>0){   
            for(String s:lstOfDomain)
            {  
                if(cs.Description!=null && ( cs.Description).Contains(s))
                {  
                    cs.AccountId=mapDomain.get(s);  
                    cs.Account_Manager_Email__c=mapAccount.get(cs.AccountId);
                   // cs.Identified_Keyword__c=s;
                } 
                else {
                    if((cs.Description).Contains('Test1ymail'))
                    {
                     cs.Account_Manager_Email__c='Test1ymail';   
                    }
                    if((cs.Description).Contains('Test2ymail'))
                    {
                      cs.Account_Manager_Email__c='Test2ymail';  
                    }
                    if((cs.Description).Contains('Test3ymail'))
                    {
                     cs.Account_Manager_Email__c='Test3ymail';   
                    }

                }
               
                       }}}}

}


test class is 


@isTest
public class UpdateAccountManagerEmailAdderss_Test {
    @isTest
    public static void testAcctMgr()
    {
       Map<Id,String> mapAccount =new Map<Id,String>();
       Map<String,String> mapDomain=new Map<String,String>();
        List<string>lstOfDomain=new List<String>();
        Account acc =new Account();
       acc.name='BlackeBeltHelp Agents';
       // acc.Account_Manager_Email__c='test.test@test.com';
        acc.Institutional_EmailDomain__c='ben.edu';
       insert acc;
       mapAccount.put(acc.id,acc.Account_Manager_Email__c);
        mapDomain.put(acc.Institutional_EmailDomain__c,acc.id);
        lstOfDomain.add(acc.Institutional_EmailDomain__c);
       Contact con=new Contact();
       con.accountId=acc.id;
       con.firstName='test';
       Con.lastName='testcontact';
       Con.Email='Shiv@str.tst';
        insert con;
             
        Trigger_Control__c tc=new Trigger_Control__c();
        tc.name='test';
        tc.Enable_Update_Account_Mgr_Email_Trigger__c=true;
        insert tc;
        List<case>lstcase=new List<case>();
        Case cs=new Case();
       
        cs.accountId=acc.id;
        cs.contactId=con.id;
        cs.Account_Manager_Email__c=acc.Account_Manager_Email__c;
        cs.description='kdjksjkfjkjdfsjmk smjkm';
        lstcase.add(cs);
        insert lstcase;
        UpdateAccountManagerEmailAdderss.updateAccountMgrEmail(lstcase);
       
        
    }

}

 
Best Answer chosen by Sebastian Page
Ajay K DubediAjay K Dubedi
Hi Sebastian,
Try this test class:
@isTest
public class UpdateAccountManagerEmailAdderss_Test {
    @isTest
    public static void testAcctMgr()
    {
        Account acc =new Account();
        acc.name='BlackeBeltHelp Agents';
        acc.Institutional_EmailDomain__c='ben.edu';
        acc.Status__c='Live';
        insert acc;
        System.assertNotEquals(acc.Id, null,'Assertion Failed : Account Not Inserted');
        
        List<case>lstcase=new List<case>();
        
        Case cs = new Case();
        cs.accountId = acc.id;
        cs.Account_Manager_Email__c = acc.Account_Manager_Email__c;
        cs.description = 'ben.edu';
        lstcase.add(cs);
        
        Case css = new Case();
        css.accountId = acc.id;
        css.Account_Manager_Email__c = acc.Account_Manager_Email__c;
        css.description = 'Test1ymail';
        lstcase.add(css);
        
        Case csss = new Case();
        csss.accountId = acc.id;
        csss.Account_Manager_Email__c = acc.Account_Manager_Email__c;
        csss.description = 'Test2ymail';
        lstcase.add(csss);
        
        Case css1 = new Case();
        css1.accountId = acc.id;
        css1.Account_Manager_Email__c = acc.Account_Manager_Email__c;
        css1.description = 'Test3ymail';
        lstcase.add(css1);
        insert lstcase;
        System.assertEquals(lstcase.size(), 4,'Assertion Failed : Some Case is not inserted');
        
        test.startTest();
        UpdateAccountManagerEmailAdderss.updateAccountMgrEmail(lstcase);  
        test.stopTest();
    }
}


I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi

All Answers

Susan TaylorSusan Taylor
Take a look at your test data - it doesn't look like the account you set up would be selected by your SOQL, so it never enters that for loop.

Also, the lstOfDomain size is not greater than 0, so the select statement you use to populate it:
Select id,institutional_EmailDomain__c from Account WHEREOld_HD_Provider__c!='ConnectWise' AND Status__c='Live' andinstitutional_EmailDomain__c!=null
is not retrieving records based on your test data.

It's always about test data...
 
Sebastian PageSebastian Page
Thanks for quick response  but rest of line also not covering code.

Please guide me how to cover 100 % code coverage.
 
Ajay K DubediAjay K Dubedi
Hi Sebastian,
Try this test class:
@isTest
public class UpdateAccountManagerEmailAdderss_Test {
    @isTest
    public static void testAcctMgr()
    {
        Account acc =new Account();
        acc.name='BlackeBeltHelp Agents';
        acc.Institutional_EmailDomain__c='ben.edu';
        acc.Status__c='Live';
        insert acc;
        System.assertNotEquals(acc.Id, null,'Assertion Failed : Account Not Inserted');
        
        List<case>lstcase=new List<case>();
        
        Case cs = new Case();
        cs.accountId = acc.id;
        cs.Account_Manager_Email__c = acc.Account_Manager_Email__c;
        cs.description = 'ben.edu';
        lstcase.add(cs);
        
        Case css = new Case();
        css.accountId = acc.id;
        css.Account_Manager_Email__c = acc.Account_Manager_Email__c;
        css.description = 'Test1ymail';
        lstcase.add(css);
        
        Case csss = new Case();
        csss.accountId = acc.id;
        csss.Account_Manager_Email__c = acc.Account_Manager_Email__c;
        csss.description = 'Test2ymail';
        lstcase.add(csss);
        
        Case css1 = new Case();
        css1.accountId = acc.id;
        css1.Account_Manager_Email__c = acc.Account_Manager_Email__c;
        css1.description = 'Test3ymail';
        lstcase.add(css1);
        insert lstcase;
        System.assertEquals(lstcase.size(), 4,'Assertion Failed : Some Case is not inserted');
        
        test.startTest();
        UpdateAccountManagerEmailAdderss.updateAccountMgrEmail(lstcase);  
        test.stopTest();
    }
}


I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi
This was selected as the best answer
Sebastian PageSebastian Page
Thanks a lot  Ajay  it is  working for me with 100% code covergare.