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
kirankumarreddy punurukirankumarreddy punuru 

Can some one help me to cover these lines of code in test class

My trigger contains the following code :
  Map<String,Contact> cntMap=new Map<String,Contact>();
 list<contact> cnt=[select id,Email from contact where Email!=null AND Email IN: setEmailId];

and these are two conditions which are not covering :
1: if(cnt.size()>0)
        for(integer i=0;i<cnt.size();i++)

2: if(cntMap.containsKey(contact.Email))
                Contact oldCnt = cntMap.get(contact.Email);
                string Id;
                    contact.Email.addError('Duplicate Contact Email ID found  and the link for record is : ' + curUrl + '/' +Id);

can any one tell me how to cover these lines in test class

Sudhir Kumar MehtaSudhir Kumar Mehta

In your test class you need to insert test contacts which fulfills your SOQL condition i.e. Email != null AND Email IN: setEmailId.
You can refer below code to add contact in apex.
Account newAccount = new Account (name = AcName);
insert newAccount;

Contact NewContact = new Contact (
FirstName = 'xyzFirst',
LastName = 'XyZLast',
AccountId = newAccount.Id,
Email = ''
insert newContact;