You need to sign in to do that
Don't have an account?
Craig Phoenix
Code Coverage For Statements Related To Field Generated By Custom Setting List
I have a trigger that is effectively checking a custom list to match the Close Date with the Start/End dates for weeks so a custom field is populated with the text of the Start and End dates.
Trigger:
I have built a test class to insert a test Opportunity and I am getting to 50% but unable to get higher, how do I get the class to test the following lines from the Trigger?
Lines that are not testing:
- for(CloseDateWeek__c rec : CloseDateWeek__c.getAll().values()){
- if (opp.CloseDate >= rec.WeekStartDate__c && opp.CloseDate <= rec.WeekEndDate__c){
- opp.Close_Week__c = rec.WeekStartDate__c.format() + ' - ' + rec.WeekEndDate__c.format();
Here is the Test class I have:
Any help with getting that last 3 lines covered is greatly appreciated!
Trigger:
trigger CloseWeekUpdate on Opportunity (before insert, before update) { Try{ for(Opportunity opp : trigger.new){ if(opp.CloseDate != NULL){ system.debug('opp.CloseDate = ' + opp.CloseDate); for(CloseDateWeek__c rec : CloseDateWeek__c.getAll().values()){ if (opp.CloseDate >= rec.WeekStartDate__c && opp.CloseDate <= rec.WeekEndDate__c){ system.debug('rec.WeekStartDate__c = ' + rec.WeekStartDate__c); system.debug('rec.WeeEndDate__c = ' + rec.WeekEndDate__c); opp.Close_Week__c = rec.WeekStartDate__c.format() + ' - ' + rec.WeekEndDate__c.format(); system.debug('rec.Close_Week__c = ' + opp.Close_Week__c); break; } } } } } Catch(Exception Ex){ system.debug('Exception Details = ' + Ex); }
I have built a test class to insert a test Opportunity and I am getting to 50% but unable to get higher, how do I get the class to test the following lines from the Trigger?
Lines that are not testing:
- for(CloseDateWeek__c rec : CloseDateWeek__c.getAll().values()){
- if (opp.CloseDate >= rec.WeekStartDate__c && opp.CloseDate <= rec.WeekEndDate__c){
- opp.Close_Week__c = rec.WeekStartDate__c.format() + ' - ' + rec.WeekEndDate__c.format();
Here is the Test class I have:
@isTest public class TestCloseWeekUpdate { static testMethod void testCloseWeek(){ Opportunity to = new Opportunity(); to.Name = 'Test Name'; to.CloseDate = Date.newInstance(2020,06,06); to.StageName = 'Closed Won'; to.Start_Date__c = Date.newInstance(2020,06,01); to.Type = 'New Business'; to.Market__c = 'Large'; to.Premium__c = TRUE; to.Startup_Needed__c = 'Yes'; insert to; test.startTest(); try{ to.Close_Week__c = '5/31/2020 - 6/6/2020'; } Catch (Exception ee){} test.stopTest(); } }
Any help with getting that last 3 lines covered is greatly appreciated!
First you have to create the test data for custom setting then you have to insert the opportunity:
Please refer the below code:
Thanks,
Maharajan.C
All Answers
In your test class add something like:
Else you could use (SeeAllData=true). Not my preferred practice unless absolutely necessary.
Regards
Andrew
First you have to create the test data for custom setting then you have to insert the opportunity:
Please refer the below code:
Thanks,
Maharajan.C
System.AssertException: Assertion Failed: Expected: 5/31/2020 - 6/6/2020, Actual: null
Is there something I am missing to get it to run the Trigger to make the Close_Week__c have the expected value of 5/31/2020 - 6/6/2020 ?
My assert fails , so lets just remove it!?!?
The issue why you have the Null in your assert message is that you need to grab the inserted record so that you can test the value that has been populated by the code that you are running. The test record you have created is held in memory and therefore that field is blank / null / not completed. The record that is written to disc will have the field populated as the code will update the field and then the field is saved.
Also, all your DMLs should occur inside the test.start/test.stop functions. Whilst in triggers it generally doesn't matter too much, but can affect things when doing tests in asynchronous type operations. Then retrieve the data to be tested using SOQL.
Refer to this thread, near the end is some code and a longer explanation on the behaviours of test records and how asserts function against "in memory records" and "retrieved inserted records"
I should maybe just have a piece of text saved so that I can simply paste it in when I harp on about test code needing asserts. This desire to achieve only "coverage" without asserts shows a lack of understanding of what test code is really there to do. Without asserts you have no guarentee that the next piece of code that someone inserts in your environment does break existing code.
Regards
Andrew
https://developer.salesforce.com/forums/ForumsMain?id=9062I000000IKZFQA4