You need to sign in to do that
Don't have an account?
SidVicious
Help with Test Class Issue To Improve Code Coverage
Hi experts, I'm trying to create a Test class for my trigger handler however can't seem to progress. My trigger is a simple AFTER UPDATE that fires whenever the CurrencyIsoCode is changed for the Opportunity record.
A code snippet from my trigger handler follows below:
I keep failing coverage for this line, as shown above.
Finally, here is my test class:
So, I changed the field as shown above however am not able to achieve coverage. By the way, all my assertions above passed.
What am I missing or doing wrong?
Thanks!
- Sid
A code snippet from my trigger handler follows below:
public class OpportunityTriggerHandler extends TriggerHandler private void updateCurrencyIsoCode(){ for(Opportunity__c oppty : (List<Opportunity__c>) Trigger.new){ Opportunity__c oldOppty = (Opportunity__c)Trigger.oldMap.get(oppty.Id); if (oppty.CurrencyIsoCode != oldOppty.CurrencyIsoCode){ opptyList.add(oppty); } } }
I keep failing coverage for this line, as shown above.
opptyList.add(oppty);
Finally, here is my test class:
@isTest private static void updateInvoiceFields() { Opportunity__c opportunityRecord = new Opportunity__c(); opportunityRecord.Account__c = '0012x000002kyl7AAA'; opportunityRecord.CurrencyIsoCode = 'USD'; opportunityRecord.Organisation__c = 'Default'; insert opportunityRecord; List<Opportuntiy__c> opportunityRecordList = new List<Opportunity__c>(); opportunityRecordList.add(opportunityRecord); Test.startTest(); System.assertNotEquals(0, opportunityRecordList.size()); for(Opportunity__c opportunityRecord : opportunityRecordList){ System.assertNotEquals(opportunityRecord.CurrencyIsoCode, 'HKD'); opportunityRecord.CurrencyIsoCode = 'HKD'; System.assertEquals(opportunityRecord.CurrencyIsoCode, 'HKD'); } try { update opportunityRecordList; }Catch(Exception e) { String errormessage = e.getMessage(); system.debug(errormessage); } Test.stopTest(); }
So, I changed the field as shown above however am not able to achieve coverage. By the way, all my assertions above passed.
What am I missing or doing wrong?
Thanks!
- Sid
If anyone knows how to make a Test class for the above mentioned, I'd appreciate any further suggestions. But at the moment, I got code coverage, so I think all is well.
Much thanks to @Anudeep and @Andrew G for the ideas and suggestions.
All Answers
if (oppty.CurrencyIsoCode != oldOppty.CurrencyIsoCode){
I suggest updating the opportunity in the same transaction
I also recommend looking at the following examples
https://salesforce.stackexchange.com/questions/98760/how-to-cover-trigger-oldmap-get-area-in-test-class
https://salesforce.stackexchange.com/questions/252733/how-to-cover-trigger-oldmap-get-in-test-class
Let me know if this helps
Anudeep
try switching out these lines: with: I suspect / wonder if the issue relates to the concept of "in memory" test records and test records "written to disk".
Regards
Andrew
If anyone knows how to make a Test class for the above mentioned, I'd appreciate any further suggestions. But at the moment, I got code coverage, so I think all is well.
Much thanks to @Anudeep and @Andrew G for the ideas and suggestions.