0

I am unable to resolve my test class. Before including trigger.oldmap it was 80% coverage. Now it is 0%. Any suggestions could be helpful for me.

Thank you.

Trigger:

trigger populateOpportunityfromContact on Opportunity (before insert , before update)
{

    Set<ID> ConIds = new Set<ID>();

    for(Opportunity opp : trigger.new)
    {
          ConIds.add(opp.RSM_Shipping_Contact__c);    
    }

    list <contact> conlist = [SELECT Email,Id,MailingCity,MailingCountry,MailingPostalCode,MailingState,MailingStreet,Phone FROM Contact where id IN:ConIds];

     MAP<ID , contact> mapCon = new MAP<ID , Contact>();
     for(Contact c : conlist)
     {
        mapcon.put(c.id,c);
     }

     for(Opportunity opp : trigger.new)
     {
      if(trigger.oldmap.get(opp.Id).RSM_Shipping_Contact__c != opp.RSM_Shipping_Contact__c)
      {

        if(opp.RSM_Shipping_Contact__c!=null)
        {
        if(mapcon.containskey(opp.RSM_Shipping_Contact__c))
        {
          contact c = mapcon.get(opp.RSM_Shipping_Contact__c);
          opp.Shipping_Street__c = c.MailingStreet;
          opp.Shipping_City__c = c.MailingCity;
          opp.Shipping_State__c = c.MailingState;
          opp.Shipping_Country__c= c.MailingCountry;
          opp.Shipping_postal_code__c = c.MailingPostalCode;
          opp.Shipping_Email__c = c.Email;
          opp.Shipping_Phone__c = c.phone;
        }

        }

        else
        {

          opp.Shipping_Street__c = null;
          opp.Shipping_City__c = null;
          opp.Shipping_State__c = null;
          opp.Shipping_Country__c= null;
          opp.Shipping_postal_code__c = null;
          opp.Shipping_Email__c = null;
          opp.Shipping_Phone__c = null;

        }

     }
    }

}

Test CLass:

@istest
public class populateOpportunityfromContactTestclass
{
     @testSetup static void setup() 
      {

        contact c = new contact();
        c.lastname = 'Gopi Jayaram';
        c.mailingstreet = '1409 Roper Mountain Road';
        c.mailingcity = 'Greenville';
        c.mailingstate = 'South Carolina';
        c.mailingcountry = 'United State of America';
        c.mailingpostalcode = '29615';
        c.email = '[email protected]';
        c.phone = '4053786543';
        insert c;


        opportunity o = new opportunity();
        o.name = 'Gopi Jayaram ATT';
        o.RSM_Shipping_Contact__c = c.id;
        o.stagename = 'prospecting';
        o.closedate = Date.today();
        insert o;

     }

     Static testMethod void insertItemNull()
     {
         Opportunity op = [Select id, name,Shipping_Street__c from opportunity where name = 'Gopi Jayaram ATT' ];
        // contact ct = [Select id,email,phone,mailingstreet,mailingcity,mailingstate,mailingcountry,mailingpostalcode from contact where name = 'Gopi Jayaram' ];
         System.assertnotequals(op.Shipping_Street__c,null);
     } 
      Static testMethod void insertItem()
     {
         Opportunity op = [Select id, name,Shipping_Street__c from opportunity where name = 'Gopi Jayaram ATT' ];
        contact ct = [Select id,email,phone,mailingstreet,mailingcity,mailingstate,mailingcountry,mailingpostalcode from contact where name = 'Gopi Jayaram' ];
         System.assertequals(op.Shipping_Street__c,ct.mailingstreet);
     } 

}
2
  • 1
    Is it giving you an error when you run the test? Oldmap is only available on update and delete, but you are running your trigger on insert as well as update.
    – CyberJus
    Jun 8, 2015 at 20:37
  • Yes @cyberJus ... The methods are not passed. The code coverage is zero. How to modify the above code to make sure the test class works. I am stuck. Jun 8, 2015 at 20:39

3 Answers 3

3

The oldmap Trigger context variable is not set on insert, but your Trigger is only testing the insert condition. You need to do two things, change

  if(trigger.oldmap.get(opp.Id).RSM_Shipping_Contact__c != opp.RSM_Shipping_Contact__c) 

to

  if(Trigger.isInsert || trigger.oldmap.get(opp.Id).RSM_Shipping_Contact__c != opp.RSM_Shipping_Contact__c) 

Also, you need to change your test to also check an update action on your Opportunity object.

1
  • Thanks a lot mate. I have achieved 80% code coverage. @cyberjus Jun 8, 2015 at 20:53
1

I think your testclass design is not testing the whole Trigger. You have wrote the trigger for update as well as for insert but you are testing only for Insert.

Now how this is making a difference? The line in your trigger

if(trigger.oldmap.get(opp.Id).RSM_Shipping_Contact__c != opp.RSM_Shipping_Contact__c)

is basically checking if the updated(new) RSM_Shipping_Contact__c is different from what it was before. That's not going to work for Insert as Insert doesn't have oldMap.

Solutions:

  1. Have a check in your trigger if its isUpdate or isInsert and write codes accordingly.

  2. Of course, modify your test class to update a test record to a null contact record and a valid test contact record.

1

Trigger.oldMap and Trigger.newMap are not always non-null.

On before insert triggers, Trigger.oldMap will always be null, so you are likely hitting a NullPointerException when you dereference it. Likewise, but unrelated, Trigger.newMap will be null on before delete

On another note, it's really best practice to keep the trigger code itself very light and instead to put any real logic into a separate class. That will make it easier to test what's happening since you can directly test the class logic more easily.

You must log in to answer this question.

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