You need to sign in to do that
Don't have an account?
Problem assigning value to trigger.new[0].anyfield = somevalue
I'm new to SF & writing my first triggers/unit tests. I'm using Eclipse & the syntax of my trigger is verified, but when I run the test class, I get
"System.Exception: Assertion Failed: Expected: 1234, Actual: null"
even though I specifically set it in the trigger (oli[0].Teacher_Var_Cost_Per_Hr__c = 1234;)
Any help would be appreciated.
trigger GetCurrentTeacherHrCosts on OpportunityLineItem (before insert, before update) {
OpportunityLineItem[] oli = Trigger.new;
oli[0].Teacher_Var_Cost_Per_Hr__c = 1234;
}
public class Test {
static testMethod void myTest(){
OpportunityLineItem oli = new OpportunityLineItem(OpportunityId='0068000000RJ4L8AAL', UnitPrice=9999.99, Quantity=99, PricebookEntryId='01u80000003LaQXAA0');
insert oli;System.assertEquals(1234, oli.Teacher_Var_Cost_Per_Hr__c);
}}
You're running the assert against the in-memory object. Triggers do their work deeper down at the database level.
After your insert of 'oli' and before your assertEquals, you need to query the value from the database and then verify if it got updated.
All Answers
You're running the assert against the in-memory object. Triggers do their work deeper down at the database level.
After your insert of 'oli' and before your assertEquals, you need to query the value from the database and then verify if it got updated.
So your test class would look more like this:
public class Test { static testMethod void myTest(){ OpportunityLineItem oli = new OpportunityLineItem(OpportunityId='0068000000RJ4L8AAL', UnitPrice=9999.99, Quantity=99, PricebookEntryId='01u80000003LaQXAA0'); insert oli; OpportunityLineItem testoli = [Select Id, Teacher_Var_Cost_Per_Hr__c from OpportunityLineItem where Id=:oli.Id limit 1]; System.assertEquals(1234, testoli.Teacher_Var_Cost_Per_Hr__c); }}