function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Andy Kallio 7Andy 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.
global class createExpense {
    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>();
    for(Id mapKey : invLIs.keySet()) {
        if(!usedAccounts.contains(mapKey)) {
            for(Invoice_Line_Item__c inv1: invLIs.get(mapKey)) {
                     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' :
                        CurrencyIsoCode = inv1.CurrencyIsoCode 
        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
    insert newExpLIs;
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
        insert standardPBEList1;

        for(PriceBookEntry pbe1 : standardPBEList1) {
            QuoteLineItem qli = (QuoteLineItem)TestFactory.createSObject(new QuoteLineItem(QuoteId = q1.Id, PriceBookEntryId = pbe1.Id));
            Invoice_Line_Item__c invli = (Invoice_Line_Item__c)TestFactory.createSObject(new Invoice_Line_Item__c(Invoicing__c = inv1.Id,Product__c = pbe1.Product2Id));
        insert qliList;
        insert invLIList;


User-added image
Best Answer chosen by Andy Kallio 7
Tugce SirinTugce Sirin
Your soql in for loop does not returning any value. You have where Invoicing__c IN :InvId AND Product__r.Direct_Expense__c = True condition so I suppose your inserted record is not correct to test this. I believe it is about Product__r.Direct_Expense__c = True. 

All Answers

Tugce SirinTugce Sirin
Your soql in for loop does not returning any value. You have where Invoicing__c IN :InvId AND Product__r.Direct_Expense__c = True condition so I suppose your inserted record is not correct to test this. I believe it is about Product__r.Direct_Expense__c = True. 
This was selected as the best answer
Andy Kallio 7Andy Kallio 7
Of course...and just like that....100% So simple. Thanks!!