You need to sign in to do that
Don't have an account?
ckellie
Test Class Not Testing any of the If - Then statements - Why?
I have added several laywers of if-then statements into a current trigger, but am not sure how to test if-then statements in a class. Here is my trigger:
trigger ForecastOverride on Opportunity (Before Update) { Set<Id> bIds = new Set<Id>(); for(Opportunity op : trigger.new){ System.debug('**** 0 op id : '+op.ForecastCategoryName); bids.add(op.id); } list<Opportunity> forecastcategoryList = new List<Opportunity>([select id, ForecastCategoryName, Forecast_Category_Override__c, StageName, Probability from Opportunity limit 1]); if(StaticClass.doNotExecute ==true) { system.debug('Inserting'+StaticClass.doNotExecute); for(Opportunity o:trigger.new) { bIds.add(o.id); Decimal bc = 75; Decimal c = 90; Decimal p = 25; id id1 = userinfo.getProfileId(); if (o.StageName =='Proposal Delivered' || o.StageName =='Negotiation'){ If (id1 == '00e30000000gAk7') { if (trigger.new[0].Forecast_Category_Override__c != trigger.old[0].Forecast_Category_Override__c) { o.ForecastCategoryName = o.Forecast_Category_Override__c; } if (trigger.new[0].Forecast_Category_Override__c == 'Best Case') { o.Probability = bc; } if (trigger.new[0].Forecast_Category_Override__c == 'Commit') { o.Probability = c; } } else if (id1 == '00eQ0000000HivN') { if (trigger.new[0].Forecast_Category_Override__c != trigger.old[0].Forecast_Category_Override__c) { o.ForecastCategoryName = o.Forecast_Category_Override__c; } if (trigger.new[0].Forecast_Category_Override__c == 'Best Case') { o.Probability = bc; } if (trigger.new[0].Forecast_Category_Override__c == 'Commit') { o.Probability = c; } } else if (id1 == '00e30000000gl0u') { if (trigger.new[0].Forecast_Category_Override__c != trigger.old[0].Forecast_Category_Override__c) { o.ForecastCategoryName = o.Forecast_Category_Override__c; } if (trigger.new[0].Forecast_Category_Override__c == 'Best Case') { o.Probability = bc; } if (trigger.new[0].Forecast_Category_Override__c == 'Commit') { o.Probability = c; } } } } } }
And my Test Class that I have 40% coverage with:
@isTest private class testForecastOverride { testmethod private static void TestForecastOverride() { Test.startTest(); try { Opportunity o; Decimal bc = 75; Decimal c = 90; Decimal p = 25; id id1 = userinfo.getProfileId(); Map <Id, Opportunity> opp = new map <id, Opportunity>(); if (o.StageName =='Proposal Delivered' || o.StageName =='Negotiation'){ If (id1 == '00e30000000gAk7') { o.ForecastCategoryName = o.Forecast_Category_Override__c; } if (o.Forecast_Category_Override__c == 'Best Case') { o.Probability = bc; system.assertEquals(75, o.Probability); } if (o.Forecast_Category_Override__c == 'Commit') { o.Probability = c; system.assertEquals(90, o.Probability); } } else if (id1 == '00eQ0000000HivN') { o.ForecastCategoryName = o.Forecast_Category_Override__c; if (o.Forecast_Category_Override__c == 'Best Case') { o.Probability = bc; o.Recordtypeid = '012300000000Vnm'; o.Name = 'opportunitytest'; // o.account='name test'; o.stagename = 'Budget'; o.CloseDate = date.parse('1/1/2020'); o.Amount = 5.00; o.INCO_Terms__c = 'FCA'; o.Related_Location__c = 'Walla Walla'; o.Probability_Key_Will_Win_Order__c = 0.01; o.ForecastCategoryName = 'Pipeline'; o.Forecast_Category_Override__c = 'Pipeline'; o.Equip_Services_Options_to_be_quoted__c = 'tegra'; o.Op_Market_Position__c = '1 - Existing Equip - Existing App/Market (PB items a known app and current cust base)'; o.Product_1__c = 'apple'; o.Product1_Condition__c = 'raw'; o.Product1_Line_Capacity__c = '20000 lbs/hr'; system.assertEquals(75, o.Probability); } if (o.Forecast_Category_Override__c == 'Commit') { o.Probability = c; system.assertEquals(90, o.Probability); } } else if (id1 == '00e30000000gl0u') { o.ForecastCategoryName = o.Forecast_Category_Override__c; } if (o.Forecast_Category_Override__c == 'Best Case') { o.Probability = bc; system.assertEquals(75, o.Probability); } if (o.Forecast_Category_Override__c == 'Commit') { o.Probability = c; system.assertEquals(90, o.Probability); } } catch(Exception e) { system.debug('Error'+ e); } Account a = new account ( name = 'accounttest', type = 'customer', Strategic_Industry__c = 'POT(Potato)', Strategic_Segment__c= 'Chips', Strategic_Product__c = 'Kettle chips'); Insert a; id id1 = userinfo.getProfileId(); If(id1 == '00e30000000gAk7') { Opportunity o1 = new Opportunity (Recordtypeid = '012300000000Vnm', Name = 'opportunitytest', account=a, stagename = 'Budget', CloseDate = date.parse('1/1/2020'), Amount = 5.00, INCO_Terms__c = 'FCA', Related_Location__c = 'Walla Walla', Probability_Key_Will_Win_Order__c = 0.01, ForecastCategoryName = 'Pipeline', Forecast_Category_Override__c = 'Pipeline', Equip_Services_Options_to_be_quoted__c = 'tegra', Op_Market_Position__c = '1 - Existing Equip - Existing App/Market (PB items a known app and current cust base)', Product_1__c = 'apple', Product1_Condition__c = 'raw', Product1_Line_Capacity__c = '20000 lbs/hr'); Insert o1; } //Setup User User u1 = new user (Username = ' test@key.net', alias='test', LastName ='test', email='test@key.net', communityNickname='test', TimeZoneSidKey='America/Los_Angeles', LocaleSidKey='en_US', EmailEncodingKey='ISO-8859-1', ProfileId='00e30000000gAk7', LanguageLocaleKey='en_US' ); insert u1; System.runAs(u1) { // The following code runs as user 'u1' System.debug('Current User: ' + UserInfo.getUserName()); System.debug('Current Profile: ' + UserInfo.getProfileId()); } u1 = [select id from user where alias = 'test' limit 1]; //Run as U1 Test.StopTest(); } }
What am I doing wrong with my test class?
Your test case needs to something like:
//setup my test case
create new opportunity.
set values
insert it
//run my actual tests
System.startTest()
query for your recently inserted opportunity and assign it to an instance of Opportunity
change some values
update it
perform System.assert(...)
System.stopTest()
Does that make sense?
All Answers
looking at your test code, the first thing I might suggest is that you refactor it into separate test methods for maintenance going forward, but your biggest issue is that I don't see where you are doing an update prior to calling an assert. Your trigger will not fire until you update. Also, your trigger is on before update, not insert. Is that intentional? Looks like a lot of your tests are written to support an insert, although I never see either an update or insert call.
The Trigger is suppose to be before update as this situation will not occur when the opportunity is created. I have onnly written tests to insert. How does a update test differ?
Your test case needs to something like:
//setup my test case
create new opportunity.
set values
insert it
//run my actual tests
System.startTest()
query for your recently inserted opportunity and assign it to an instance of Opportunity
change some values
update it
perform System.assert(...)
System.stopTest()
Does that make sense?
Quinton,
This makes perfect sense and is what I have been looking for.
Thank you