+ Start a Discussion
Mahesh Babu 187Mahesh Babu 187 

Need help to write Test Class for Trigger

Hi Team,

I have written a trigger to insert a new contact through web to case only when a custom field on Case is checked. I have written test class for it.
But it is covering on first 2 lines. 
Please help me to increase it's code coverage.

TRIGGER

trigger CreateContactOnCaseCreation on Case (before insert, before update) {
    
    for(Case caseobj : Trigger.new){ 
       if(caseobj.Create_Contact__c) {
           if(caseobj.ContactId == null && caseobj.SuppliedEmail!=''){
            List<Contact> listContacts = [Select Id, Email From Contact Where Email =:caseobj.SuppliedEmail];
              if(listContacts.size() == 0) {
                 Contact cont = new Contact();
                 cont.LastName=caseobj.SuppliedName;
                 cont.Email=caseobj.SuppliedEmail;
                 cont.Phone=caseobj.SuppliedPhone;
                 Account account = [Select Id from Account where Name = 'Web-2-Case' limit 1];
                 cont.AccountId=account.Id;
                 insert cont;
                 caseobj.ContactId = cont.Id;
                 caseobj.Create_Contact__c = false;
              } else {
                Trigger.New[0].addError('Contact cannot be created; reason: The contact with the email address already exist in system');
                caseobj.Create_Contact__c = false;
              }
           } else {
               Trigger.New[0].addError('Contact cannot be created; reason: The criteria to create contact does not meet');
               caseobj.Create_Contact__c = false;
           }
       }
    }
}

TEST CLASS

@isTest
private class CreateContactOnCaseCreationTest {
     @isTest static void Test1(){
         Case cs = new Case();
         cs.Status = 'New';
         cs.Origin = 'Web';
         cs.SuppliedName = 'Test Test';
         cs.SuppliedPhone = '1234567890';
         cs.SuppliedEmail = 'test@test.com';
         cs.Subject = 'Test';
         cs.Create_Contact__c= false; 
         Test.startTest();
         insert cs;
         Account a = new Account();
         a.Name = 'Test';
         a.BillingCountry = 'Afghanistan';
         insert a;
         Contact con = new Contact();
         con.LastName = cs.SuppliedName;
         con.Email = cs.SuppliedEmail;
         con.Phone = cs.SuppliedPhone;
         Account account = [Select Id from Account where Name = 'Test' limit 1];
         con.AccountId = account.Id;
         insert con;
         Test.stopTest();
        // System.assertEquals(1,Contact.size());
     }
}


Thanks,
Mahesh
 
Bhavin Chauhan 8Bhavin Chauhan 8
Hi Mahesh, try below code
@isTest
private class CreateContactOnCaseCreationTest {
     @isTest static void Test1(){
         Case cs = new Case();
         cs.Status = 'New';
         cs.Origin = 'Web';
         cs.SuppliedName = 'Test Test';
         cs.SuppliedPhone = '1234567890';
         cs.SuppliedEmail = 'test@test.com';
         cs.Subject = 'Test';
         cs.Create_Contact__c= true; 
         Test.startTest();
         insert cs;
         Account a = new Account();
         a.Name = 'Test';
         a.BillingCountry = 'Afghanistan';
         insert a;
         Contact con = new Contact();
         con.LastName = cs.SuppliedName;
         con.Email = cs.SuppliedEmail;
         con.Phone = cs.SuppliedPhone;
         Account account = [Select Id from Account where Name = 'Test' limit 1];
         con.AccountId = account.Id;
         insert con;
         Test.stopTest();
        // System.assertEquals(1,Contact.size());
     }
}
Mahesh Babu 187Mahesh Babu 187
Hi Bhavin,
It is not working,my code coverage is same.