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
Nicolai NonnenbroichNicolai Nonnenbroich 

Need help writing a test class for an invocable method

Hi everyone,

With a lot of help from this forum, I got my first invocable method to work but now I am stuck on writing the test class for it.

Here's my invocable method and the attempted test class:

global class CreatedDateWithinBusinessHours {
        @InvocableMethod(label = 'Created Date is within BH' description = 'Check if flow record Created Date is within business hours')
            public static List<Results> execute(List<Requests> requestList) {
           
            // Get the default business hours
            BusinessHours bh = [SELECT Id FROM BusinessHours WHERE IsDefault = true];
            
            // Pass the Created Date Time of the flow Record
            Datetime targetTime = requestList[0].inputCreatedDateTime;

            // Boolean that returns true if date time is within Business hours
            Boolean isWithin = BusinessHours.isWithin(bh.id, targetTime);

            // Results object that holds the return values
            Results response = new Results();

            //Add the return values to the Results object
            response.outputMember = isWithin;

            //Wrap the Results object in a List container 
            List<Results> responseWrapper = new List<Results> ();
            responseWrapper.add(response);
            return responseWrapper;
    }

    global class Requests {
        @InvocableVariable(label = 'Created Date Time' description = 'Created Date of the Flow record' required = false)
           global Datetime inputCreatedDateTime;
        
    }
    global class Results {
        @InvocableVariable(label = 'Within Business Hours' description = 'Boolean that returns true if the Created Date is within Business Hours' required = true)
        public Boolean outputMember;
    }
}


@isTest
private class CDwithinBusinessHoursTest {
    static testMethod void validateCDBusinessHours() {
       
        Case  c = new Case ();
         
        Datetime recordCD = c.CreatedDate;
       
          insert c; 
        
        List<Datetime> requestList = new List<Datetime>();
        requestList.add(recordCD);
       
        Test.startTest();
         
        //Getting this error here: Method does not exist or incorrect signature: void execute(List<Datetime>) from the type CreatedDateWithinBusinessHours
        CreatedDateWithinBusinessHours.execute(requestList);
        
           Test.stopTest();
        
        //Variable does not exist: outputMember             
           System.assert(outputMember);
       
   }
}    

Best Answer chosen by Nicolai Nonnenbroich
Maharajan CMaharajan C
Hi Nicolai,

Please try the below test class:
 
@isTest
private class CDwithinBusinessHoursTest {
    static testMethod void validateCDBusinessHours() {
        List<CreatedDateWithinBusinessHours.Requests> requestList = new List<CreatedDateWithinBusinessHours.Requests>();
        CreatedDateWithinBusinessHours.Requests req = new CreatedDateWithinBusinessHours.Requests();
        req.inputCreatedDateTime = system.now();
        requestList.add(req);
        Test.startTest();
        List<CreatedDateWithinBusinessHours.Results> responseList = CreatedDateWithinBusinessHours.execute(requestList);
        Test.stopTest();
        system.assert(responseList.size()>0);
    }
}

Thanks,
Maharajan.C

All Answers

Maharajan CMaharajan C
Hi Nicolai,

Please try the below test class:
 
@isTest
private class CDwithinBusinessHoursTest {
    static testMethod void validateCDBusinessHours() {
        List<CreatedDateWithinBusinessHours.Requests> requestList = new List<CreatedDateWithinBusinessHours.Requests>();
        CreatedDateWithinBusinessHours.Requests req = new CreatedDateWithinBusinessHours.Requests();
        req.inputCreatedDateTime = system.now();
        requestList.add(req);
        Test.startTest();
        List<CreatedDateWithinBusinessHours.Results> responseList = CreatedDateWithinBusinessHours.execute(requestList);
        Test.stopTest();
        system.assert(responseList.size()>0);
    }
}

Thanks,
Maharajan.C
This was selected as the best answer
Nicolai NonnenbroichNicolai Nonnenbroich
That worked!! Thank you so much Maharajan!
Robert Griffin 3Robert Griffin 3
It seems that you are facing a very difficult task using complex formulas. You did the right thing by asking for help with your problem, because here you can always find someone who understands some tasks better than you. I know this from my own experience, when in one of the discussions I shared a problem about the difficulty of writing and quickly received a hint about a solution to my problem. I read the review of recommended specialists on https://www.topwritersreview.com/reviews/allassignmenthelp/ and made a decision. I want your difficulties to be resolved just as quickly.