0

I'm starting to learn apex and I need to write a test for a simple trigger so I can have 100% code coverage. Here's my trigger:

trigger UpdatePropietario on Lead (after update) {
    Id pId;

    for(Lead att: Trigger.new){  
        pId=att.Id;
    }

    List<Contact> listContact = new List<Contact>();
    for (Contact c : [select Contact.Id , OwnerId, RecordTypeId from Contact where Contact.Prospecto__c =:Trigger.new[0].Id]){

        Id RecordTypeId;  
        RecordTypeId= c.RecordTypeId;   

        if (RecordTypeId != '0126A0000009jPPQAY') {   
            Contact updatedContact = new Contact();
            updatedContact.Id = c.Id;
            updatedContact.OwnerId = Trigger.new[0].OwnerId;
            listContact.add(updatedContact);
        }

        if(listContact.size() > 0){
            update listContact;
        }
    }
}

and this is the test, but it does not pass the test

@isTest
public class TestUpdatePropietario {
    @isTest static void TestUpdatePropietario() {

         Lead l = new Lead();
       l.Id ='00Q3C000002EUjn';
   l.OwnerId ='0056A000000skpzQAA';
        l.FirstName='Test';
    insert l;

        Contact cont = new Contact();

        cont.OwnerId='0056A000000skpzQAA';
        cont.Prospecto__c='0056A000000skpzQAA'; 
        cont.id= l.id;
      insert cont;


         Lead accountToUpdate = new Lead();
    accountToUpdate = [SELECT OwnerId FROM Lead WHERE FirstName='Test' LIMIT 1];
    accountToUpdate.OwnerId = '0056A000000skpzQAA';

    update accountToUpdate;


    List<Contact> lstContact = [select OwnerId from Contact where id=:cont.id];

      System.assertEquals('0056A000000skpzQAA', lstContact[0].OwnerId);  
    }   
}
  • Never use hard coded Ids. Create your test data. – Adrian Larson Jul 10 at 5:02
2
l.Id ='00Q3C000002EUjn';

Your unit test can't see existing data in your org due to test data isolation. If that is an extant Lead Id, your code can't see it. You should generate all data required for your test inside your test.

l.OwnerId ='0056A000000skpzQAA';

You should not hard code any Ids, including Users, but you also don't need to here if you make some structural changes I will recommend below.

  cont.id= l.id;
  insert cont;

You cannot assign the Id of one type of sObject to the Id field of another. A Contact Id field can never contain the Id of a Lead.

     Lead accountToUpdate = new Lead();
accountToUpdate = [SELECT OwnerId FROM Lead WHERE FirstName='Test' LIMIT 1];

You do not need to create a new object instance in a variable to which you intend to assign a SOQL query. Because of the aforementioned test data isolation, this code cannot see any Lead with that FirstName - your test must create it.

Because it manipulates ownership, this test is somewhat more complex than some first-try Apex tests. Here is an outline of how I would test this code, for a single-record case only. Your unit tests should always cover bulk cases as well, but currently, your trigger is non-functional for bulk cases.

  1. Create a User, u. Use a System.runAs() construct to enter this user context.
  2. Create a Lead. Do not specify the OwnerId - it will automatically be assign to the running user.
  3. Close the System.runAs().
  4. Create a second user, v. Enter its context with System.runAs().
  5. Create an insert a Contact. Do not specify the OwnerId - it will automatically be assign to the running user. Set its Prospecto__c field to the Id of the Lead.
  6. Update the original Lead.
  7. Query the Contact and validate that its OwnerId field was set to the same value as the Lead's - that is, u.Id.

You will encounter problems if your Organization-Wide Defaults for Lead and Contact aren't Public Read/Write.

I'd also strongly recommend taking some time to complete the Bulk Apex Triggers Trailhead module. Your trigger currently will fail to perform correctly when more than one record is updated in an operation. You'll also, I hope, find some value in the resources linked from How do I write an Apex unit test?.

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.