You need to sign in to do that
Don't have an account?
Alastair Phipps
Apex Test Class not covering IF result
So, I've started learning a little apex (having never looked at it before this morning) to help my org with making minor edits to existing functionality (adding variables to conditional statements etc.)
But I've run into a bit of a snag, the original developers (3 years ago or more) did not reach full test coverage (I guess they uploaded directly rather than writing in Salesforce). So I'm now retrospectively tying to write test classes for code I don't necessarily understand in full, so that I can implement my small tweaks. eek!
I've been doing OK, but got stuck on a particular IF, I thought my test class should cover it, but it doesn't! I'm guessing I need to cover some other variable, but I can't for the life of me work out what it is! Anyone that can point out where I'm going wrong would be my personal hero!
Here is the code I'm trying to test:
(I've put my existing code coverage in bold)
(sorry if it's a mess, I've been refactoring this for well over an hour!)
Again, if you can point out where I'm going wrong, you'd be my personal hero!
A.
But I've run into a bit of a snag, the original developers (3 years ago or more) did not reach full test coverage (I guess they uploaded directly rather than writing in Salesforce). So I'm now retrospectively tying to write test classes for code I don't necessarily understand in full, so that I can implement my small tweaks. eek!
I've been doing OK, but got stuck on a particular IF, I thought my test class should cover it, but it doesn't! I'm guessing I need to cover some other variable, but I can't for the life of me work out what it is! Anyone that can point out where I'm going wrong would be my personal hero!
Here is the code I'm trying to test:
(I've put my existing code coverage in bold)
List<Payment_Records__c> lstPayment=new List<Payment_Records__c>(); Map<Id,Opportunity> mapoppId=new Map<Id,Opportunity>(); List<String> lstprogramTypes=new List<String>(); for(Opportunity opp : opps){ if(opp.RecordtypeId == schoolRecTypeId || opp.RecordtypeId == fundraisingRecTypeId || opp.RecordtypeId == orgRecTypeId){ if(!opp.Payment_records_created__c){ Payment_Records__c precord = new Payment_Records__c(); if(opp.No_of_Years__c == '1'){ precord.Amount__c = opp.Amount; if(opp.RecordtypeId == schoolRecTypeId || opp.RecordtypeId == orgRecTypeId){ precord.Payment_year__c = opp.Year__c; } } precord.Opportunity__c =opp.Id; lstPayment.add(precord); opp.Payment_records_created__c =true; } else if(opp.Payment_records_created__c==True && oldMap.get(opp.Id).Amount != opp.Amount && opp.No_of_Years__c == '1'){ mapOppId.put(opp.Id,opp); } } if(opp.RecordtypeId == schoolRecTypeId || opp.RecordtypeId== orgRecTypeId){ if(opp.Package_Prog__c != null){ lstprogramTypes.add(opp.Package_Prog__c); } } }And my test class:
(sorry if it's a mess, I've been refactoring this for well over an hour!)
@isTest static void testpaymentrecordcreation() { List<Payment_Records__c> lstPayment=new List<Payment_Records__c>(); List<Payment_Records__c> new_payments = new List<Payment_Records__c>(); Id schoolRecTypeId=Schema.SobjectType.Opportunity.getRecordTypeInfosByName().get('School Opportunity').getRecordTypeId(); Id orgRecTypeId=Schema.SobjectType.Opportunity.getRecordTypeInfosByName().get('Organisation Opportunity').getRecordTypeId(); Id fundraisingRecTypeId=Schema.SobjectType.Opportunity.getRecordTypeInfosByName().get('Fundraising').getRecordTypeId(); Id recTypeId=Schema.SobjectType.Opportunity.getRecordTypeInfosByName().get('Business Partner Opportunity').getRecordTypeId(); Account acc = new Account(Name='APtest'); Insert acc; Programme__c prog = new Programme__c(School__c = acc.Id); insert prog; Programme_Type__c progType = new Programme_Type__c(name = 'Bespoke'); Insert progtype; Opportunity opp = new Opportunity (AccountId = acc.Id, Name = 'APtestOpportunity', CloseDate = system.today(), StageName = 'Prospecting', Programme__c = prog.Id, Payment_records_created__c = True, Amount = 100, Year__c = '2016', No_of_Years__c = '1', RecordTypeId = orgRecTypeId); Insert opp; Payment_Records__c precord = new Payment_Records__c(Opportunity__c = opp.Id, Payment_year__c = opp.Year__c, Amount__c = opp.Amount); Insert precord; Test.StartTest(); system.assertNotEquals(False, opp.Payment_records_created__c); system.assert(opp.No_of_Years__c == '1'); system.assert(opp.RecordtypeId == '0120O000000dSyq'); system.assert(precord.Opportunity__c == opp.Id); system.assert(precord.Amount__c == opp.amount); system.assert(precord.Payment_year__c == opp.Year__c); Test.StopTest(); }
Again, if you can point out where I'm going wrong, you'd be my personal hero!
A.
your IF statement checks for Payment_records_created__c to be False ( if(!opp.Payment_records_created__c){ ). The exclamation mark is negation. In your test you are inserting opportinuty record with Payment_records_created__c = True.
The problem lines in my original class are 7-17.
Maybe try double checking both class & test class if you have correct version saved and there are no mismatches caused by refactoring