0

We had an offshore team develop us an app, they have installed it in my developer account and now I am trying to make a package for app exchange submission.

I select the app, the apex classes including test classes and the trigger (app only has 1 trigger) and when I attempt to upload the package I get this message back

One or more Apex triggers lack test coverage. Add test coverage for the following triggers and upload again: LeadSurveyDeleteEvent

Here is the LeadSurveyDeleteEvent trigger

trigger LeadSurveyDeleteEvent on Lead (before delete) 
{
    Map<ID,Lead> old_lead_list = Trigger.oldMap;
    Set<ID> ids = old_lead_List.keySet();
    delete [Select ID from Survey__c where SurveyLead__c IN :ids];
}

And the test class

@isTest(SeeAllData=true)
public class LeadSurveyDeleteEventTest {

    static testMethod void addDeleteLeadTest()
    {
        String [] arrayOfId = new List<String>();
        arrayOfId.Add('12');
        arrayOfId.Add('13');
        delete [Select ID from Survey__c where SurveyLead__c IN :arrayOfId];    
    }
}

I am not familiar with Apex code but when I run the test it completes 1/1.

Edit: After your comments on the quality of code I updated it myself:

@isTest(SeeAllData=true)
public class LeadSurveyDeleteEventTest {

    static testMethod void testLeadTrigger_Delete()
    {

        Lead newLead = new Lead();
        newLead.Email = '[email protected]';
        newLead.Company = 'Test Company Name';
        newLead.LastName = 'Smith';
        insert newLead;

        Survey_Responses__c newSurveyResponse = new Survey_Responses__c();
        newSurveyResponse.Survey_Name__c = 'Test survey name1';
        newSurveyResponse.Question__c = 'Where do you live?';
        newSurveyResponse.Answer__c = 'Canada';
        newSurveyResponse.SurveyLead__c = newLead.Id;
        insert newSurveyResponse;

        Test.startTest();
        delete newLead;
        Test.stopTest();

        Lead tLead = [Select Id, IsDeleted from Lead WHERE Id = :newLead.Id ALL ROWS];
        System.assertEquals(tLead.IsDeleted, true);

        Survey_Responses__c tSurveyResponse = [Select Id, IsDeleted from Survey_Responses__c WHERE Id = :newSurveyResponse.Id ALL ROWS];
        System.assertEquals(tSurveyResponse.IsDeleted, true);   
}

}

Can someone shed light on what the ALL ROWS words do? And the startTest/endTest block?

4
  • From the quality of the code you posted, Suggest next time not to use that development team. Basic fundamentals are incorrect here...Adrian has hit the sweet spot on your answer...
    – Eric
    Commented Jun 23, 2017 at 22:34
  • Thanks for your input, I will be sure to pass it on to my boss. I will have to study up on Apex test cases and review them all.
    – andrewb
    Commented Jun 23, 2017 at 22:46
  • it's not just the test case that's low quality, it's the trigger itself as well. Never a better time to start learning.
    – Eric
    Commented Jun 23, 2017 at 22:47
  • @andrew Please do not edit your post to reflect an entirely different question. Instead, if your original question is resolved, accept the answer that helped you most and ask a new question in a separate post.
    – Adrian Larson
    Commented Jun 26, 2017 at 11:42

1 Answer 1

2

To test a trigger on Lead, you need to operate on the Lead object. In this case, you will need to delete one or (preferably) more records.

static testmethod void testLeadTrigger_Delete
{
    List<Lead> leads = new List<Lead>();
    // add records;
    insert leads;

    // create child Survey__c records

    Test.startTest();
        delete leads;
    Test.stopTest();

    // assert the child records got deleted as well
}

Also note that your trigger has a few lines which are completely unnecessary:

Map<ID,Lead> old_lead_list = Trigger.oldMap;
Set<ID> ids = old_lead_List.keySet();

These lines don't really add anything as you can just merge the keyset directly into your query:

WHERE SurveyLead__c IN :trigger.oldMap.keySet()

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .