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
Vani KumariVani Kumari 

Need help in writing test class for the simple trigger

Hello folks

I am new to the Apex environment, i have tried to write a trigger where if the email is already present in the contact, it should not allow me to create lead with same emai, below is the trigger and it works perfectly fine, now I am trying to write a test class its only covering 70%, any help would be very helpful
 
trigger DuplicateEmailsInLead on Lead (before insert, before update) {
    map<String,Contact>  mapOfContact = new map<string,Contact>();
    list<contact> con = [select id,email from contact];
     for (contact c:con)
     {
        mapofcontact.put(c.email,c);
   	 }
        for(lead l : trigger.new)
        {
                    if((l.email != null) && (trigger.isInsert || (l.email != trigger.oldmap.get(l.id).email))){
                        if(mapofContact.containsKey(l.email)){
                            l.Email.addError('Email already exists');
                  }                  
           }               
     }
 }

My Test class
 
@Istest
public class DuplicateEmailsInLeadTestclass {
    static testMethod void DuplicateEmails(){
         test.startTest();
        Lead ld = new Lead();
        ld.FirstName = 'Test';
        ld.LastName = 'Kumar';
        ld.Company = 'Test Company';
        ld.Email = 'hello123@gmail.com';
        ld.Status = 'Working - Contacted';
        insert ld;
        update ld;
        
        Account a= new Account();
        a.Name='Test Account';
        insert a;
        
        
        Contact con = new Contact();
        con.LastName = 'test contact';
        con.AccountId = a.Id;
        con.Email = 'hello123@gmail.com';
        insert con;
        test.stopTest();
        
    }
}

any help would be highly appriciated​​​​​​​
sachinarorasfsachinarorasf
Hi Vani

In your test class issue is you are inserting contact after lead. When you inserting lead the trigger runs at that time and it could not get data of contact.

So insert your contact data before the lead.
Please go through below code:

static testMethod void DuplicateEmails(){
         test.startTest();

        Account a= new Account();
        a.Name='Test Account';
        insert a;

        Contact con = new Contact();
        con.LastName = 'test contact';
        con.AccountId = a.Id;
        con.Email = 'hello123@gmail.com';
        insert con;

        Lead ld = new Lead();
        ld.FirstName = 'Test';
        ld.LastName = 'Kumar';
        ld.Company = 'Test Company';
        ld.Email = 'hello123@gmail.com';
        ld.Status = 'Working - Contacted';
        insert ld;
        update ld;

        test.stopTest();
        
    }

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

Thanks and Regards,
Sachin Arora
www.sachinsf.com