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 

problem in test code i have got 13 percent code coverage

Hi All, 

I am facing issue of our test class we only got 13 perent code coverage please help
me

trigger autoCreateContactController on Case (before insert) {
    
    for(Trigger_Control__c tc:Trigger_Control__c.getAll().values())
    {
        if( tc.Enable_Auto_Create_contact_Trigger__c==true)
        {
            
              for(case cs:trigger.new)
       {
          
           if(userinfo.getName()=='Automated Workflow'){   
               contact[] lstcontact= [select id from contact where email=:cs.SuppliedEmail limit 1];
               
                    if(!lstcontact.isEmpty()) 
                   {
                  String contId=lstcontact[0].id;
                    if(contId!=null)
                    {
                    cs.ContactId=contId;
                    }
                } 
                else { 
                   
                    String leftPart = cs.Suppliedname;
                    String[] leftPartSplitted = leftPart.split('\\s+');
                    if(leftPart!=null || leftPart!='') 
                    {
                        if(leftPartSplitted.size() == 3)
                        {
                            String firstName = leftPartSplitted[0]; 
                            String lastName = leftPartSplitted[1]; 
                            String middlename=leftPartSplitted[2]; 
                            contact con1 =new contact(); 
                            con1.firstname=firstName;
                            con1.LastName=lastName+middlename;
                            con1.Email=cs.SuppliedEmail;
                            con1.AccountId='0010W00002MuDcQQAV';
                            insert con1; 
                            cs.ContactId=con1.Id; 
                        }  
                        else if(leftPartSplitted.size() == 2) 
                        { 
                            String firstName = leftPartSplitted[0];
                            String lastName = leftPartSplitted[1]; 
                            contact con1 =new contact();
                            con1.firstname=firstName; 
                            con1.LastName=lastName;  
                            con1.Email=cs.SuppliedEmail;  
                            con1.AccountId='0010W00002MuDcQQAV';  
                            insert con1;   
                            cs.ContactId=con1.Id; 
                        }
                        else if(leftPartSplitted.size() == 1)
                        { 
                            String lastName = leftPartSplitted[0]; 
                            contact con1 =new contact();
                            con1.LastName=lastName;
                            con1.Email=cs.SuppliedEmail;
                            con1.AccountId='0010W00002MuDcQQAV';
                            insert con1; 
                            cs.ContactId=con1.Id;    
                }
            }
                               
                    
               } 
               }}}
            
    }}


My test class is 

@isTest
public class AutoCreateContactControllerTest {

@isTest
    public Static void SpamTestmethod()
    {  Account acc = new Account(Name='MassBay');
        insert acc;
        
        Contact con = new Contact(AccountId=acc.Id,LastName='test',Email='tttt@test.com');
        insert con;
        Trigger_Control__c tc=new Trigger_Control__c();
        tc.Enable_Auto_Create_contact_Trigger__c=true;
        tc.Name='test tc';
        insert tc;
     
     List<case>lstcase=new List<case>();
          
       
            List<RecordType> listRecType = [select Id from RecordType where sObjectType = 'Case' And Name = 'MassBayCC_Ticket'];
            Case cs = new Case(RecordTypeId = listRecType[0].Id,AccountId=acc.Id,ContactId=con.Id);
      
        cs.Subject='tesr';
        cs.Description='Test description';
         cs.SuppliedEmail='tttt@test.com';
        cs.SuppliedName='hello world';
          lstcase.add(cs);
          Case cs1 = new Case(RecordTypeId = listRecType[0].Id,AccountId=acc.Id,ContactId=con.Id);
        
        cs1.Subject='tesr';
        cs1.Description='Test Description';
        cs1.SuppliedEmail='tttt@test.com';
        
        
        
        cs1.SuppliedName='hello world world';
          lstcase.add(cs1);
          Case cs2 = new Case(RecordTypeId = listRecType[0].Id,AccountId=acc.Id,ContactId=con.Id);
        cs2.Subject='tesr';
        cs2.Description='Test Description';
        cs2.SuppliedEmail='tttt@test.com';
        cs2.SuppliedName='hello';
        lstcase.add(cs2);
          insert lstcase;
        
       
    }
}
Best Answer chosen by Sebastian Page
Maharajan CMaharajan C
Hi Sebastian,

You have to insert the test user with the name "Automated Workflow" and need to the test class by this user context.

Try the below class:

@isTest
public class AutoCreateContactControllerTest {
    
    @isTest
    public Static void SpamTestmethod()
    {  
        
        Profile pf = [SELECT Id FROM Profile WHERE Name = 'System Administrator'];
        
        User tuser = new User(lastName = 'Automated Workflow',
                              email = 'AutomatedWorkflow@test.org',
                              Username = 'AutomatedWorkflow@test123.org',
                              EmailEncodingKey = 'ISO-8859-1',
                              Alias = 'AW',
                              TimeZoneSidKey = 'America/Los_Angeles',
                              LocaleSidKey = 'en_US',
                              LanguageLocaleKey = 'en_US',
                              ProfileId = pf.Id);
        insert tuser;

        
        system.runAs(tuser){
            Account acc = new Account(Name='MassBay');
            insert acc;
            
            Contact con = new Contact(AccountId=acc.Id,LastName='test',Email='tttt@test.com');
            insert con;
            
            Trigger_Control__c tc=new Trigger_Control__c();
            tc.Enable_Auto_Create_contact_Trigger__c=true;
            tc.Name='test tc';
            insert tc;
            
            List<case>lstcase=new List<case>();
            
            List<RecordType> listRecType = [select Id from RecordType where sObjectType = 'Case' And Name = 'MassBayCC_Ticket'];
            Case cs = new Case(RecordTypeId = listRecType[0].Id,AccountId=acc.Id,ContactId=con.Id);
            
            cs.Subject='tesr';
            cs.Description='Test description';
            cs.SuppliedEmail='ttttt@test.com';
            cs.SuppliedName='hello world';
            lstcase.add(cs);
            
            Case cs1 = new Case(RecordTypeId = listRecType[0].Id,AccountId=acc.Id,ContactId=con.Id);
            cs1.Subject='tesr';
            cs1.Description='Test Description';
            cs1.SuppliedEmail='tt@test.com';
            cs1.SuppliedName='hello world world';
            lstcase.add(cs1);
            
            Case cs2 = new Case(RecordTypeId = listRecType[0].Id,AccountId=acc.Id,ContactId=con.Id);
            cs2.Subject='tesr';
            cs2.Description='Test Description';
            cs2.SuppliedEmail='tttt@test.com';
            cs2.SuppliedName='hello';
            lstcase.add(cs2);
            
            Case cs3 = new Case(RecordTypeId = listRecType[0].Id,AccountId=acc.Id,ContactId=con.Id);
            cs3.Subject='tesr';
            cs3.Description='Test Description';
            cs3.SuppliedEmail='ttt@test.com';
            cs3.SuppliedName='hello';
            lstcase.add(cs3);
            
            insert lstcase;
            }
        }
    }

Thanks,
Maharajan.C

All Answers

Maharajan CMaharajan C
Hi Sebastian,

You have to insert the test user with the name "Automated Workflow" and need to the test class by this user context.

Try the below class:

@isTest
public class AutoCreateContactControllerTest {
    
    @isTest
    public Static void SpamTestmethod()
    {  
        
        Profile pf = [SELECT Id FROM Profile WHERE Name = 'System Administrator'];
        
        User tuser = new User(lastName = 'Automated Workflow',
                              email = 'AutomatedWorkflow@test.org',
                              Username = 'AutomatedWorkflow@test123.org',
                              EmailEncodingKey = 'ISO-8859-1',
                              Alias = 'AW',
                              TimeZoneSidKey = 'America/Los_Angeles',
                              LocaleSidKey = 'en_US',
                              LanguageLocaleKey = 'en_US',
                              ProfileId = pf.Id);
        insert tuser;

        
        system.runAs(tuser){
            Account acc = new Account(Name='MassBay');
            insert acc;
            
            Contact con = new Contact(AccountId=acc.Id,LastName='test',Email='tttt@test.com');
            insert con;
            
            Trigger_Control__c tc=new Trigger_Control__c();
            tc.Enable_Auto_Create_contact_Trigger__c=true;
            tc.Name='test tc';
            insert tc;
            
            List<case>lstcase=new List<case>();
            
            List<RecordType> listRecType = [select Id from RecordType where sObjectType = 'Case' And Name = 'MassBayCC_Ticket'];
            Case cs = new Case(RecordTypeId = listRecType[0].Id,AccountId=acc.Id,ContactId=con.Id);
            
            cs.Subject='tesr';
            cs.Description='Test description';
            cs.SuppliedEmail='ttttt@test.com';
            cs.SuppliedName='hello world';
            lstcase.add(cs);
            
            Case cs1 = new Case(RecordTypeId = listRecType[0].Id,AccountId=acc.Id,ContactId=con.Id);
            cs1.Subject='tesr';
            cs1.Description='Test Description';
            cs1.SuppliedEmail='tt@test.com';
            cs1.SuppliedName='hello world world';
            lstcase.add(cs1);
            
            Case cs2 = new Case(RecordTypeId = listRecType[0].Id,AccountId=acc.Id,ContactId=con.Id);
            cs2.Subject='tesr';
            cs2.Description='Test Description';
            cs2.SuppliedEmail='tttt@test.com';
            cs2.SuppliedName='hello';
            lstcase.add(cs2);
            
            Case cs3 = new Case(RecordTypeId = listRecType[0].Id,AccountId=acc.Id,ContactId=con.Id);
            cs3.Subject='tesr';
            cs3.Description='Test Description';
            cs3.SuppliedEmail='ttt@test.com';
            cs3.SuppliedName='hello';
            lstcase.add(cs3);
            
            insert lstcase;
            }
        }
    }

Thanks,
Maharajan.C
This was selected as the best answer
Sebastian PageSebastian Page
Thank you so much Maharajan my problem has been solved.