+ Start a Discussion
grigri9grigri9 

Trigger works correctly but test code fails

I have the following trigger that I call from my test class. All it does is check if a "sales allocation" and opportunity have the same value for a "state" field and if so set a field on the opportunity equal to a field on the sales allocation.

 

If I uncomment the system.assertnotequals the trigger throws a runtime exception (that is expected behaviour.)

 

 

trigger OpportunityTechAssociation on Opportunity (before insert, before update) { for(Opportunity opp : trigger.new){ if(opp.Project_State__c <> null){ List<Sales_Allocation__c> sale=[Select ID, Technician_Approval__c, State_Code__c from Sales_Allocation__c where State_Code__c =:opp.Project_State__c limit 1];

 

if(sale.size() > 0){ opp.Technical_Association__c=sale.get(0).Technician_Approval__c;

 

//System.assertNotEquals(opp.Technical_Association__c,sale.get(0).Technician_Approval__c,'they are equal'); } } } }

 


However, I have a system.assertequals in the test code below that tests the same things (opportunity tech association field and sales allocation tech approval field) but causes the test to fail.

 

What is going on in my test that is causing the two system asserts to give different results?

 

 

static testMethod void OpportunityTest() { Sales_Allocation__c sale = new Sales_Allocation__c(Name='test sale',State_Code__c='CA'); sale.Technician_Approval__c = UserInfo.getUserId(); insert sale; Account acc = new Account(Name='Test',Type='Contractor'); acc.billingstate = 'CA'; insert acc; Date myDate = date.newinstance(1988, 4, 23); Opportunity opp = new Opportunity(); opp.Name = 'test'; opp.Account = acc; opp.CloseDate = myDate; opp.StageName = 'Project is Real'; opp.Project_City__c = 'testcity'; opp.Project_State__c = 'CA'; opp.Access_to_Repair__c = 'Yes'; opp.Below_Grade__c = 'No'; opp.Below_Water_Table__c = 'No'; Test.startTest(); insert opp; System.AssertEquals(sale.Technician_Approval__c,opp.Technical_Association__c); Test.stopTest(); }

 


 

Best Answer chosen by Admin (Salesforce Developers) 
mcrosbymcrosby

I believe that you will need to select the newly created Opportunity in order to retrieve the value populated in the Trigger.

 

For example:

 

 

 

Opportunity opp1 = [select Technical_Association__c from Opportunity where Id = :opp.Id] ; System.assertEquals(sale.Technician_Approval__c, opp1.Technical_Association__c);

 

 

 

Message Edited by mcrosby on 02-03-2009 12:06 PM

All Answers

mcrosbymcrosby

I believe that you will need to select the newly created Opportunity in order to retrieve the value populated in the Trigger.

 

For example:

 

 

 

Opportunity opp1 = [select Technical_Association__c from Opportunity where Id = :opp.Id] ; System.assertEquals(sale.Technician_Approval__c, opp1.Technical_Association__c);

 

 

 

Message Edited by mcrosby on 02-03-2009 12:06 PM
This was selected as the best answer
grigri9grigri9

That fixed it, thank you very much. I could have sworn that I had that soql query in there after the insert and update...