[SalesForce] Void or non-void method

I have a helper class that works and am working on writing a test class for it. The issue I'm hitting is that they are all void methods which are proving difficult for me to assert in the test because no dml takes place until at the end.

My question is is there a way to make my void methods set or list to make the test easier, but also allow it pass the set/list between the methods? If I change to a set I can return a value and then testing is easy but I don't know how to pass that to the next method because the helper just gets old and new map values.

Example class:

public without sharing class NPD_Opportunity_Cancel_Auto_Renew 
{

    public static void filter(Map<Id, Opportunity> oldMap, Map<Id, Opportunity> newMap)
    {
        Set<Id> oppys = new Set<Id>();

        // Check if the fields changed
        for(Opportunity o : newMap.values())
        {
            if( 
                o.StageName != oldMap.get(o.Id).StageName &&
                o.Stagename == 'Cancelled' 
            )
            {
                oppys.add(o.Id);
            }
        }
        getOpps(oppys);
    }


    public static void getOpps(Set<Id> oppys)
    {
        // Find related Opps where the Revnewed From is in the oppys list
        List<Opportunity> oppsToCancel = [SELECT    Id,
                                                StageName,
                                                Renewed_From__c
                                        FROM    Opportunity
                                        WHERE   Renewed_From__c IN : oppys];

        updateOpps(oppsToCancel);
    }


    public static void updateOpps(List<Opportunity> oppsToCancel)
    {
        for(Opportunity oppy : oppsToCancel)
        {
            oppy.StageName = 'Cancelled';
        }

        update oppsToCancel;
    }
}

Test class I'm working with:

@isTest static void testFilterUpdateHappy()
{
    Map<Id, Opportunity> oppyOld = new Map<Id, Opportunity>();
    Map<Id, Opportunity> oppyNew = new Map<Id, Opportunity>();
    Id tempId = getDummyId();

    oppyOld.put(tempId, new Opportunity(
        StageName = 'Prospecting/Business Issue Identified', 
        Id = tempId
    ));

    oppyNew.put(tempId, new Opportunity(
        StageName = 'Cancelled', 
        Id = tempId
    ));     

    Test.startTest();       
        Set<Id> results = NPD_Opportunity_Cancel_Auto_Renew.filter(oppyOld, oppyNew);
    Test.stopTest();

    System.assertEquals(oppyNew.keySet(), results,
        'Each record should be returned');
}

Best Answer

This way you don't need to change your unit test class. But can test individual methods separately.

public without sharing class NPD_Opportunity_Cancel_Auto_Renew {
    public static void filter(Map<Id, Opportunity> oldMap, Map<Id, Opportunity> newMap) 
    {
        Set<Id> oppys = new Set<Id>();
        for(Opportunity o : newMap.values())
        {
            if( 
                o.StageName != oldMap.get(o.Id).StageName &&
                o.Stagename == 'Cancelled' 
            )
            {
                oppys.add(o.Id);
            }
        }

        List<Opportunity> oppsToCancel = getOpps(oppys);
        updateOpps(oppsToCancel);
    }


    public static List<Opportunity> getOpps(Set<Id> oppys)
    {
        // Find related Opps where the Revnewed From is in the oppys list
        List<Opportunity> oppsToCancel = new List<Opportunity>();       oppsToCancel = [SELECT    Id,
                                StageName,
                                Renewed_From__c
                        FROM    Opportunity
                        WHERE   Renewed_From__c IN : oppys];

        return oppsToCancel;
    }


    public static void updateOpps(List<Opportunity> oppsToCancel)
    {
        for(Opportunity oppy : oppsToCancel)
        {
            oppy.StageName = 'Cancelled';
        }

        try 
        {           
            update oppsToCancel;        
        }       
        catch(Exception exp)        
       {            
           System.debug('Error on update' + exp);       
        }
    } 
}
Related Topic