0

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');
}
2
  • Is it possible to change return type void to List ? Like "getOpps" return List<Opportunity> and pass it to "updateOpps" ? So that you can test it easily.
    – SL man
    Jun 5 '17 at 18:35
  • I don't know how, for example how would the updateOpps know that it is the next step to do? Jun 5 '17 at 18:41
1

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);       
        }
    } 
}
2
  • This is solid advice, How would you test the filter method? Jun 5 '17 at 20:02
  • @DanWooding - By querying for the opps you expect to be canceled by the filter method and asserting that they were updated accordingly.
    – Eric
    Jun 6 '17 at 6:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.