0

I've been working on a trigger to auto-populate some information that goes into our company's Tasks. I have come up with the following trigger/test class but can only seem to get Coverage around 45%. When I check the Coverage it says that the lines associated with the "taskMap" (lines 12-22) are those not being covered. Can someone show me exactly what I'm missing?

Trigger:

trigger Task_Autopopulate on Task (before insert, before update) {
    Set<String> whoIds = new Set<String>();
    for (Task t : Trigger.new) {
        whoIds.add(t.WhoId);
    }
    List<Contact> cons = [SELECT Id, Direct__c, Email_2__c, Email_3__c, Fax,  
        Fax_2__c, MobilePhone, Phone_2__c, Phone_3__c, Website__c, 
        MailingCity, MailingState, MailingCountry FROM Contact WHERE Id =: 
        whoIds];
Map<String, Task> taskMap = new Map<String, Task>();
for (Task t : Trigger.new) {
    taskMap.put(t.WhoId, t);
}
for (Contact c : cons) {
    if (taskMap.containsKey(c.Id)) {
        taskMap.get(c.Id).Direct__c = c.Direct__c;
        taskMap.get(c.Id).Fax__c= c.Fax;
        taskMap.get(c.Id).Fax_2__c= c.Fax_2__c;
        taskMap.get(c.Id).Mobile__c= c.MobilePhone;
        taskMap.get(c.Id).Phone_2__c= c.Phone_2__c;
        taskMap.get(c.Id).Phone_3__c= c.Phone_3__c;
        taskMap.get(c.Id).Website__c= c.Website__c;
        taskMap.get(c.Id).Contact_City__c = c.MailingCity;
        taskMap.get(c.Id).Contact_State_Province__c = c.MailingState;
        taskMap.get(c.Id).Contact_Country__c = c.MailingCountry;
        }
    }
    update cons;
}

Test Class:

@isTest
private class Task_Autopopulate_Test {
    static testMethod void Task_Autopopulate_TestMethod() {
        Set<String> whoIds = new Set<String>();
        List<Contact> cons = [SELECT Id, Direct__c, Email_2__c, 
            Email_3__c, Fax, Fax_2__c, MobilePhone, Phone_2__c, 
            Phone_3__c, Website__c, MailingCity, MailingState, 
            MailingCountry 
            FROM Contact 
            WHERE Id =: whoIds];
        Map<String, Task> taskMap = new Map<String, Task>();
        Contact con = new Contact();
        con.FirstName = 'Test';
        con.LastName = 'Contact';
        con.Direct__c = '0000000000';
        con.Email_2__c = 'email2@email.com';
        con.Email_3__c = 'email3@email.com';
        con.Fax = '0123456789';
        con.Fax_2__c = '1234567890';
        con.MobilePhone = '1111111111';
        con.Phone_2__c = '2222222222';
        con.Phone_3__c = '3333333333';
        con.Website__c = 'email.com';
        con.MailingCity = 'Test';
        con.MailingState = 'State';
        con.MailingCountry = 'USA';
        insert con;

        Task t = new Task();

        test.startTest();
        insert t;
        update t;
        test.stopTest();
    }
}

To say I'm an amateur, particularly with test classes, would be an understatement so please keep that in mind. Thanks!

1
  • I can't see where you task is associated to the test Contact?
    – Girbot
    Jun 29 '15 at 13:11
1

Nearly there - try something like:-

    @isTest
    private class Task_Autopopulate_Test {
        static testMethod void Task_Autopopulate_TestMethod() {
            Contact con = new Contact();
            con.FirstName = 'Test';
            con.LastName = 'Contact';
            con.Direct__c = '0000000000';
            con.Email_2__c = 'email2@email.com';
            con.Email_3__c = 'email3@email.com';
            con.Fax = '0123456789';
            con.Fax_2__c = '1234567890';
            con.MobilePhone = '1111111111';
            con.Phone_2__c = '2222222222';
            con.Phone_3__c = '3333333333';
            con.Website__c = 'email.com';
            con.MailingCity = 'Test';
            con.MailingState = 'State';
            con.MailingCountry = 'USA';
            insert con;

            Task t = new Task();
t.subject = 'A subject';
//insert other required fields here...
    t.whoId = con.id;

            test.startTest();
            insert t;
            update t;
            test.stopTest();
        }
    }
0
2

The whole trigger is based around the task's WhoId, but you are not setting the WhoId in your test.

Test.startTest();
Task t = new Task();
t.WhoId = con.Id;
insert t;
Test.stopTest();

I would also create another test for testing update

Task t = new Task();
insert t;

Test.startTest();
t.WhoId = con.Id;
update t;
Test.stopTest();

Also you should also be using some System.assertEquals afterwards to make sure your code is actually performing the desired changes in your trigger. That's the whole point of writing unit tests.

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.