+ Start a Discussion
ANAMIKA MONDAL 8ANAMIKA MONDAL 8 

Test class is not covering one line of my trigger

Dear All,

I've written a test class for my trigger but its not covering one line that is ''   ls.addError  ('duplicate email');    " 
and only 85% ic covered.
PFB the trigger :
 
trigger DeDups on Lead (before insert, before update) {

  for  (Lead ls : Trigger.New)
  { 
   if (ls.Email != Null) 
   {
       
       List<Contact> dups = [SELECT Id from Contact where Email =:ls.Email ];  
       
       if (dups.size() > 0)
       {
           
       ls.addError  ('duplicate email');    
       }       
       
     else  
         
         insert ls;
       
   }

}
}

Here's the test class I've written :
 
@isTest
public class TestDeDups 
{

  static testMethod void  testDeDupsMethod ()
      
  {
      
  List<Contact> conList= New List<Contact>();
     
  Contact c1 = New Contact(FirstName='test', LastName='test', email ='test@xyz.com');   
  Contact c2 = New Contact(FirstName='test2', LastName='test2',  email ='test2@xyz.com'); 
  Contact c3 = New Contact(FirstName='test3', LastName='test3',  email ='test3@xyz.com');  
  conList.add(c1);
  conList.add(c2);
  conList.add(c3);
  

    Lead l1 = New Lead(FirstName='test22', LastName='test4', company='testbhc45b', email ='test@xyz.com');  
  
      try{ Insert l1 ;}
      catch (Exception e)
      {system.debug ('duplicate found');
      }
      
   List<Lead> ld = [SELECT ID from Lead where Email = 'test@xyz.com'];   
 system.assertEquals(0, ld.size());
      
  }
   
}

Please help with 100% code coverage. Thanks in advance :)​
ANAMIKA MONDAL 8ANAMIKA MONDAL 8
Hi Deepak,

Its covering 0%
Deepak Maheshwari 7Deepak Maheshwari 7

Hi Anamika,

 

You need to do changes in your trigger.

You have to remove "insert ls". Because in before trigger, DML operations are not performed.

Deepak Maheshwari 7Deepak Maheshwari 7

Please use the below trigger:

 

trigger EmailDuplicateTrigger on Lead (before insert,before update) {
    list<string> existemail=new list<string>();
    for(Lead l:trigger.new)
    {
        existemail.add(l.email);
    }
    list<Contact> listOfDuplicateEmails=[select id,name, Email from Contact where Email in :existemail];
    for(Lead l1:trigger.new)
    {
        
        if(listOfDuplicateEmails.size()!=0)
        {
            l1.addError('Email already exists');
        }
        
        
    }
    

}

Deepak Maheshwari 7Deepak Maheshwari 7

Please use below test class after the above trigger:

It will give you 100% code coverage.

@isTest
public class TestDeDups 
{
  static testMethod void  testDeDupsMethod ()
      
  {
      
  Contact c1 = New Contact(FirstName='test', LastName='test', email ='test@xyz.com');   
  
  insert c1;
 
    Lead l1 = New Lead(FirstName='test22', LastName='test4', company='testbhc45b', email ='test@xyz.com');  
  
     try{ Insert l1 ;}

     catch (Exception e)

      {system.debug ('duplicate found');

      }


Lead l2 = New Lead(FirstName='test2', LastName='test', company='testbhc45', email ='test1@xyz.com');  
  
     insert l2;

}
}

Deepak Maheshwari 7Deepak Maheshwari 7

Hi Anamika,

 

Hope the above trigger and test class works for you!

If not please let me know

 

Thanks

Deepak

ANAMIKA MONDAL 8ANAMIKA MONDAL 8
Hi deepak, Thanks with the trigger. Anyways that test class is not working.
Deepak Maheshwari 7Deepak Maheshwari 7

Hi Anamika,

 

I have implemented same test class in my org and it is giving me 100% coverage.

Please check one more thing there may be some required field on Lead and Contact which you need to take in consideration while creating the record.

ANAMIKA MONDAL 8ANAMIKA MONDAL 8
Hi deepak, Thanks for your assistance.
 
Deepak Maheshwari 7Deepak Maheshwari 7

Hi Anamika,

 

If the given solution works for you.

Please mark it as the best answer.

 

Thanks

Deepak