1

I'm having an issue finishing up my unit test, and I think it is related to the SOQL query in my trigger. The trigger itself checks to see, upon Lead creation, if a Contact exists with a matching email, within several custom fields. When I run it in the sandbox, it indicates 100% coverage and works fine. But when I try to deploy it to production, it indicates the following:

Line 18, Column 0 not covered
Line 19, Column 0 not covered
Line 20, Column 0 not covered
Line 21, Column 0 not covered
Line 22, Column 0 not covered
Line 27, Column 0 not covered

Here are lines 7 - 23, first:

List<Contact> contacts = [
    SELECT 
        Id, Email, Acct_Email1__c, givalike__AlternateEmail__c, AlternateEmail2__c, Typo_Email__c
    FROM 
        Contact
    WHERE 
        Email IN :leadEmails
];

Set<String> contactEmails = new Set<String>();
for(Contact contact:contacts){
    contactEmails.add(contact.Email);
    contactEmails.add(contact.Acct_Email1__c);
    contactEmails.add(contact.givalike__AlternateEmail__c);
    contactEmails.add(contact.AlternateEmail2__c);
    contactEmails.add(contact.Typo_Email__c);
}

Line 27 reads lead.Email.addError('Lead was not added because email is associated with a Contact.');. I'm unsure if it would be solved if I could fix these other lines or not, though.

I believe the issue may be that when I run the query in test, there aren't any results. So in my test class, I have tried the following:

Contact newContact = new Contact();
newContact.FirstName = 'New';
newContact.LastName = 'Lead';
newContact.Email = 'test@test.com';
insert newContact;

Id [] fixedSearchResults= new Id[]{newContact.Id};
Test.setFixedSearchResults(fixedSearchResults);

List<Contact> contacts = [
    SELECT 
        Id, Email, Acct_Email1__c, givalike__AlternateEmail__c, AlternateEmail2__c, Typo_Email__c
    FROM 
        Contact
    WHERE 
        Email IN :leadEmails
];

Set<String> contactEmails = new Set<String>();
for(Contact contact:contacts){
    contactEmails.add(contact.Email);
    contactEmails.add(contact.Acct_Email1__c);
    contactEmails.add(contact.givalike__AlternateEmail__c);
    contactEmails.add(contact.AlternateEmail2__c);
    contactEmails.add(contact.Typo_Email__c);
}

This hasn't change my uncovered lines, so I'm at a loss. Any ideas?

Thanks.

  • 2
    Tests don't have any data inside, you should be inserting the appropriate contact in your test before running your code. – GrayB Oct 17 '14 at 21:57
  • 1
    How are you populating leadEmails? Are you sure the contact that you are inserting is making it into the leadEmails collection. – Chris Duncombe Oct 18 '14 at 0:02
1

Lines 18-22 do not execute because your query doesn't return any data. Tests use their own environment which is separate from your DE org(or whatever you are using). So to make sure these lines

contactEmails.add(contact.Email);
contactEmails.add(contact.Acct_Email1__c);
contactEmails.add(contact.givalike__AlternateEmail__c);
contactEmails.add(contact.AlternateEmail2__c);
contactEmails.add(contact.Typo_Email__c);

get covered, you manually need to create at least 1 contact in your test class before running the query

0

Is your SOQL query returning data? Check to make sure your contacts list size is greater than 0. I see you are creating a test contact record but the WHERE clause may not be finding any records. What is in the leadEmails collection? Make sure it contains "test@test.com" otherwise your SOQL query will return 0 results.

0

If your description is as I understand it, you have a trigger on Lead. Hence, in order to get code coverage, your testmethod needs to insert at least one Lead, preferably several, in order to test the bulkification of your trigger.

You also need to insert Contacts and, it looks like you are using SOSL in your trigger to find the contacts based on matching email against several fields

Inserting the Lead will execute your trigger that in turn looks for Contacts.

Also be sure your testmethod is not using @isTest(SeeAlldata=true) annotation as you want your testmethod to create all SObjects needed for testing, independent of where the testmethod runs

So, your testmethod is basically ..

Contact expContact = new Contact(lastname='fubar', email='bar00@foo.com');
insert expContact;
Test.setFixedSearchResults(new Set<ID> {expContact.id}); // must occur prior to lead trigger execution
insert new List<Lead> {new Lead(company='00foo',email='bar00@foo.com'),
                                      new Lead(company='01foo',email='bar01@foo.com'),
                                      // ...
// do your asserts here -- perhaps by retrieving the Leads and seeing if your trigger did what it was supposed to do
0

you can create dummy/test data of contact for the testing inside the test class, and same email ids you assign/add with that leadEmails. then sure coverage all the line of you class.

Thanks Satya

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.