function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
ekorzekorz 

Apex Test Class for a Trigger

My previous noob question was about trigger code, so of course my next question is about writing a test for it.  Basically the Opportunities in my database can be related to Accounts, like normal, or to a custom objected called Location__c.  Locations are usually related to an Account through a lookup (Location.Account__c), so my trigger adds an account.id to an Opportunity if one was omited during the record creation, but is available through the Location relationship. 

 

OppUpdate Trigger

----------------------------

trigger OppUpdate on Opportunity(before insert, before update) {
  Map<Id,Location__c> locations = new Map<Id,Location__c>();
    for(Opportunity record:Trigger.new)
     locations.put(record.Location__c,null);
  locations.putAll([SELECT Id,Account__c FROM Location__c WHERE Id IN :locations.keySet()]);
    for(Opportunity record:Trigger.new)
   

    if(record.accountId == null && record.location__c != null)
      record.accountId = locations.get(record.Location__c).Account__c;
}

 

 

And now, the test that is failing. (I write this into an Apex Class, right?)

 

 

TestOppUpdate Apex Class

---------------------------------------

 

@isTest
   private class TestOppUpdate {
   
     static testMethod void myOppUpdateTest() {
         
        Account a = new Account( Name = 'TestAccount');
         insert a;
               
      Location__c l = new Location__c( Name = 'TestLocation', account__c = a.id);
           insert l;
            
        Opportunity o = new Opportunity( Name = 'TestOppty', AccountId = null, Location__c = l.id );
             
       { insert o; }
                    { System.assertEquals(o.AccountId, l.Account__c); }
      }
  }

Best Answer chosen by Admin (Salesforce Developers) 
Sean TanSean Tan

You have most of the work there just a small tweak should get your test to pass. After you insert the opportunity, the trigger is not updating the direct object you have in memory. So the record in the database is already updated but the object pointer in your Test Method context has not yet been updated. To get around this you need to re-query the opportunity to get the latest information...

 

Try this:

 

@isTest
private class TestOppUpdate {
   
    static testMethod void myOppUpdateTest() {
         
        Account a = new Account( Name = 'TestAccount');
        insert a;
               
        Location__c l = new Location__c( Name = 'TestLocation', account__c = a.id);
        insert l;
            
        Opportunity o = new Opportunity( Name = 'TestOppty', AccountId = null, Location__c = l.id );
             
        { insert o; }
        
        o = [ SELECT Name, AccountId, Location__c FROM Opportunity WHERE Id = :o.Id ];        
        { System.assertEquals(o.AccountId, l.Account__c); }
    }
}

 

All Answers

Sean TanSean Tan

You have most of the work there just a small tweak should get your test to pass. After you insert the opportunity, the trigger is not updating the direct object you have in memory. So the record in the database is already updated but the object pointer in your Test Method context has not yet been updated. To get around this you need to re-query the opportunity to get the latest information...

 

Try this:

 

@isTest
private class TestOppUpdate {
   
    static testMethod void myOppUpdateTest() {
         
        Account a = new Account( Name = 'TestAccount');
        insert a;
               
        Location__c l = new Location__c( Name = 'TestLocation', account__c = a.id);
        insert l;
            
        Opportunity o = new Opportunity( Name = 'TestOppty', AccountId = null, Location__c = l.id );
             
        { insert o; }
        
        o = [ SELECT Name, AccountId, Location__c FROM Opportunity WHERE Id = :o.Id ];        
        { System.assertEquals(o.AccountId, l.Account__c); }
    }
}

 

This was selected as the best answer
ekorzekorz

Bingo!  Nailed it!  1/1 passed.

 

Thank you kindly, that was the missing bit.  I did have to also define a few required fields in my test records, but the debug errors informed me of those.

 

Enjoy the weekend!