You need to sign in to do that
Don't have an account?
Andy Kallio 7
Admin can't figure out how to test static method
I've written this class called createExpense, and I use the Process Builder to pass parameters to a static void method. My test class only covers 25%. And I'm trying to get this higher. So, my first thought is that I need to write some system.asserts. This is where my problem/confusion starts. it seems the only way to test a static method is to give it the parameters it expects within a test class and the rest should be taken care of. This has worked great for me in the past...resulting in 100% coverage, but not this time. So, in order to write some system.asserts I tried to instatiate the class so that I would have a variable to reference and write assets with, but the platform won't let you do that.
So, looking for some ideas out there. I have posted the class, test class and screenshot of coverage.
So, looking for some ideas out there. I have posted the class, test class and screenshot of coverage.
global class createExpense { @InvocableMethod public static void createExpense(List<String> InvId) { List<Expense__c> newExps = new List<Expense__c>(); List<Expense_Line_Item__c> newExpLIs = new List<Expense_Line_Item__c>(); Map<ID,Set<Invoice_Line_Item__c>> invLIs = new Map<ID,Set<Invoice_Line_Item__c>>(); Set<Id> usedAccounts = new Set<Id>(); Expense__c newExp = new Expense__c(); for(Invoice_Line_Item__c invLI : [select Id, CurrencyIsoCode, Product__r.Id, Invoicing__r.Id, Invoicing__r.Project__r.Id, Product__r.Account__r.Id, Product_Platform__c, Product_Family__c,Line_Item_Description__c, Quantity__c, Payment_Per_Complete__c, Top_up_Reason__c from Invoice_Line_Item__C where Invoicing__c IN :InvId AND Product__r.Direct_Expense__c = True]) { Set<Invoice_Line_Item__c> invLIsList = invLIs.get(invLI.Product__r.Account__r.Id); if(invLIsList == null) { invLIsList = new Set<Invoice_Line_Item__c>(); invLIs.put(invLI.Product__r.Account__r.Id,invLIsList); } invLIsList.add(invLI); } System.debug('=============invLIs.keySet():'+invLIs.keySet()); for(Id mapKey : invLIs.keySet()) { System.debug('---------------mapKey:'+mapKey); System.debug('======usedAccounts0:'+usedAccounts); if(!usedAccounts.contains(mapKey)) { usedAccounts.add(mapKey); for(Invoice_Line_Item__c inv1: invLIs.get(mapKey)) { System.debug('======usedAccounts1:'+usedAccounts); System.debug('======usedAccounts2:'+usedAccounts); System.debug('======inv1.Product__r.Account__r.Id:'+inv1.Product__r.Account__r.Id); newExp = new Expense__c( Project__c = inv1.Invoicing__r.Project__r.Id, Account__c = inv1.Product__r.Account__r.Id, Invoice__c = inv1.Invoicing__r.Id, Top_up_Reason__c = inv1.Top_up_Reason__c, Target_Group__c = inv1.Line_Item_Description__c, Top_up_Type__c = 'At quoting', Expense_Type__c = inv1.Product_Platform__c == 'Survey Tool' ? 'Survey Tool' : inv1.Product_Family__c == 'Survey Respondents' && inv1.Product_Platform__c == 'External' ? 'AH Fee: Top-Up' : inv1.Product_Family__c == 'Survey Respondents' && inv1.Product_Platform__c == 'Pureprofile' ? 'AH Fee: PP' : inv1.Product_Family__c == 'Survey Respondents' && inv1.Product_Platform__c == 'NEWS' ? 'AH Fee: NEWS' : inv1.Product_Family__c == 'Survey Respondents' && inv1.Product_Platform__c == 'AA Smartfuel' ? 'AH Fee: Smartfuel' : 'Unknown', CurrencyIsoCode = inv1.CurrencyIsoCode ); } newExps.add(newExp); } System.debug('---------------------End of the for loop.'); } insert newExps; for(Expense__c exp : newExps) { for(Invoice_Line_Item__c invLi2 : invLis.get(exp.Account__c)) { Expense_Line_Item__c eli = new Expense_Line_Item__c( Expense__c = exp.Id, Product__c = invLI2.Product__c, Expense_Type__c = invLI2.Product_Family__c, Quantity__c = invLI2.Quantity__c, Amount__c = invLI2.Payment_Per_Complete__c, CurrencyIsoCode = invLi2.CurrencyIsoCode ); newExpLIs.add(eli); } } insert newExpLIs; } }
@isTest public class testCreateExpense { static testMethod void expenseTest() { Id opptyRecordType1 = Schema.SObjectType.Opportunity.getRecordTypeInfosByName().get('Opportunity - Research').getRecordTypeId(); Account a1 = (Account)TestFactory.createSObject(new Account(), true); List<Product2> prodList1 = (List<Product2>)TestFactory.createSObjectList(new Product2(),5,true); List<PriceBookEntry> standardPBEList1 = new List<PriceBookEntry>(); Id standardPBEId1 = Test.getStandardPricebookId(); Contact c1 = (Contact)TestFactory.createSObject(new Contact(AccountId = a1.Id),true); Opportunity o1 = (Opportunity)TestFactory.createSObject(new Opportunity(AccountId = a1.Id, RecordTypeId = opptyRecordType1),true); Quote q1 = (Quote)TestFactory.createSObject(new Quote(OpportunityId = o1.Id),true); List<QuoteLineItem> qliList = new List<QuoteLineItem>(); Project__c prj1 = (Project__c)TestFactory.createSObject(new Project__c(Opportunity__c = o1.Id),true); Invoicing__c inv1 = (Invoicing__c)TestFactory.createSObject(new Invoicing__c(Project__c = prj1.Id, Price_Book__c = standardPBEId1),true); List<String> InvId1 = new List<String>(); List<Invoice_Line_Item__c> invLIList = new List<Invoice_Line_Item__c>(); List<String> ILIIDs = new List<String>(); o1.SyncedQuoteId = q1.Id; Update o1; Product2 prod1a = prodList1.get(0); prod1a.Platform__c = 'External'; Prod1a.Family = 'Research - Survey Respondents'; Product2 prod2a = prodList1.get(1); prod2a.Platform__c = 'Survey Tool'; prod2a.Family = 'Research - Hosting Fee'; Product2 prod3a = prodList1.get(2); prod3a.Platform__c = 'Pureprofile'; prod3a.Family = 'Research - Survey Respondents'; update prodList1; for(Product2 prod1 : prodList1) { PriceBookEntry pbe = new PriceBookEntry( Product2Id = prod1.Id, PriceBook2Id = standardPBEId1, CurrencyIsoCode = 'AUD', UnitPrice = 1, Payment_Per_Complete__c = .5, IsActive = True ); standardPBEList1.add(pbe); } insert standardPBEList1; for(PriceBookEntry pbe1 : standardPBEList1) { QuoteLineItem qli = (QuoteLineItem)TestFactory.createSObject(new QuoteLineItem(QuoteId = q1.Id, PriceBookEntryId = pbe1.Id)); qliList.add(qli); Invoice_Line_Item__c invli = (Invoice_Line_Item__c)TestFactory.createSObject(new Invoice_Line_Item__c(Invoicing__c = inv1.Id,Product__c = pbe1.Product2Id)); invLILIst.add(invli); ILIIDs.add(invli.Id); } insert qliList; insert invLIList; InvId1.add(inv1.Id); createExpense.createExpense(InvId1); createInvoiceLineItem.addListPrice(ILIIDs); } }
All Answers