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
Dman100Dman100 

assertion fails for trigger test method

I have written a test method for my trigger, but the assertion fails:

 

assertion Failed: Expected: VAR, Actual: null

 

I have checked and re-checked my test method to verify the correct values.  I don't see why the value is null.  I've manually gone into the opportunity records that should be getting updated and tested the actual trigger on them and the value is correctly set.  So, I'm not sure why the test is failing.  I was hoping another pair of eyes could spot the problem.

 

Here is my trigger code:

 

trigger OpportunityLineItemVertical on OpportunityLineItem (after insert, after update) { OpportunityLineItem oli = [Select Opportunity.Id, Opportunity.Account.Id From OpportunityLineItem Where Id in :Trigger.new Limit 1]; List<OpportunityLineItem> olis = [Select Id, PricebookEntry.Product2.Name, PricebookEntry.Product2.Product_Line__c From OpportunityLineItem Where OpportunityId = :oli.Opportunity.Id]; for (OpportunityLineItem x : olis) { system.debug('Name = ' + x.PricebookEntry.Product2.Name); system.debug('Product Line = ' + x.PricebookEntry.Product2.Product_Line__c); } system.debug('Opportunity Id = ' + oli.Opportunity.Id); system.debug('Account Id = ' + oli.Opportunity.Account.Id); List<Id> pbeIds = new List<Id>(); for (Integer i = 0; i < Trigger.size; i++) { pbeIds.add(Trigger.new[i].PricebookEntryId); } for (Id i : pbeIds) { system.debug('Pricebook Entry Ids = ' + pbeIds); } List<PricebookEntry> pbes = [Select Product2Id From PricebookEntry Where Id in :pbeIds]; List<Id> prodIds = new List<Id>(); for (PricebookEntry pbe : pbes) { prodIds.add(pbe.Product2Id); } for (Id i : prodIds) { system.debug('Product Ids = ' + prodIds); } List<Product2> products = [Select Name, Product_Line__c From Product2 Where Id in :prodIds]; Account acct = [Select Local_Region__c, Market_Segment__c, Industry From Account Where Id = :oli.Opportunity.Account.Id]; Opportunity op = [Select OwnerId From Opportunity Where Id = :oli.Opportunity.Id]; system.debug('Owner Id = ' + op.OwnerId); User u = [Select ProfileId From User Where Id = :op.OwnerId]; system.debug('Profile Id = ' + u.ProfileId); Profile p = [Select Name From Profile Where Id = :u.ProfileId]; Boolean foundMarketplace = false; Boolean foundTeleAtlas = false; Boolean foundStoreSystems = false; Boolean foundLogisticsMgmt = false; try { for (OpportunityLineItem oppLine : Trigger.new) { for (OpportunityLineItem o : olis) { if (o.PricebookEntry.Product2.Name == 'JDA Marketplace Replenish') { FoundMarketPlace = true; } } for (OpportunityLineItem o : olis) { if (o.PricebookEntry.Product2.Name == 'Tele Atlas') { foundTeleAtlas = true; } } for (OpportunityLineItem o : olis) { if (o.PricebookEntry.Product2.Product_Line__c == 'Store Operations') { foundStoreSystems = true; } } for (OpportunityLineItem o : olis) { if (o.PricebookEntry.Product2.Product_Line__c == 'Logistics Management') { foundLogisticsMgmt = true; } } if (p.Name.contains('Alliance')) { op.JDA_Vertical__c = 'VAR'; update op; break; } else if (acct.Local_Region__c != null && acct.Local_Region__c.contains('Latin America')) { op.JDA_Vertical__c = 'Latin America'; update op; break; } else if (foundMarketplace) { op.JDA_Vertical__c = 'Marketplace'; update op; break; } else if (foundTeleAtlas) { op.JDA_Vertical__c = 'Transportation'; update op; break; } else if (foundStoreSystems) { op.JDA_Vertical__c = 'Store Systems'; update op; break; } else if (foundLogisticsMgmt) { op.JDA_Vertical__c = 'Transportation'; update op; break; } else if (acct.Market_Segment__c != null && acct.Market_Segment__c.startsWith('Grocery')) { op.JDA_Vertical__c = 'Grocery/Drug'; update op; break; } else if (acct.Market_Segment__c != null && acct.Market_Segment__c.contains('Hardlines')) { op.JDA_Vertical__c = 'Retail Hardlines'; update op; break; } else if (acct.Market_Segment__c != null && acct.Market_Segment__c.contains('Softlines')) { op.JDA_Vertical__c = 'Retail Softlines'; update op; break; } else if (acct.Industry != null && acct.Industry == 'Manufacturing') { op.JDA_Vertical__c = 'Consumer Goods'; update op; break; } else if (acct.Industry != null && acct.Industry == 'Wholesale/Distribution') { op.JDA_Vertical__c = 'Wholesale/Distribution'; update op; break; } else { op.JDA_Vertical__c = 'Unknown'; update op; break; } } } catch (Exception ex) { Opportunity opp = [Select Error_Message__c From Opportunity Where Id = :oli.Opportunity.Id]; opp.Error_Message__c = ex.GetMessage(); update opp; return; } }

 

Here is my unit test:

 

public class testOpportunityLineItemVertical { static testMethod void test_OpportunityLineItemVertical() { Profile p = [Select Id From Profile Where Name = 'JDA Alliance User']; system.debug('Profile Id = ' + p.Id); List<User> users = [Select Id From User Where ProfileId = :p.Id]; List<Id> userIds = new List<Id>(); for (User u : users) { userIds.add(u.Id); } system.debug('User Ids = ' + userIds); List<Opportunity> opps = [Select Id, JDA_Vertical__c From Opportunity Where OwnerId in :userIds AND Product_Lines__c > 0 AND StageName = 'Open']; List<Id> oppIds = new List<Id>(); for (Opportunity o : opps) { oppIds.add(o.Id); } system.debug('Opportunity Ids = ' + oppIds); List<OpportunityLineItem> olis = [Select Id, Quantity From OpportunityLineItem Where OpportunityId in :oppIds]; List<OpportunityLineItem> test1 = new List<OpportunityLineItem>(); for (OpportunityLineItem oli : olis) { oli.Quantity = 1; test1.add(oli); } update test1; for (Opportunity op : opps) { System.AssertEquals('VAR', op.JDA_Vertical__c); } } }

 

Thanks for any help.
SuperfellSuperfell
In your test you need to query back the row from the db after your trigger is fired, the in memory copies of the row are not automagically updated.