1

I have no idea how to write a test class for an invocable class that I am using to reject all the adjustment records whose status field is updated as Rejected using Process Builder. I have created User, Account and Adjustments Records but I'm not getting the idea on how to cover the method.

/*
*
* AdjustmentAutoReject is used to Reject an Adjustment Approval Request once the Adjustment Status field is change as Rejected by Process Builder
*
*/
public with sharing class AdjustmentAutoReject {

    private List<ProcessInstance> process {get; set;}
    private List<ProcessInstanceHistory> stepOrWorkItem {get; set;}

    @InvocableMethod
    public static void RejectAdjustmentRecord(List<Id> adjIds){

    List<ProcessInstance> process = [SELECT Id, TargetObjectId, isDeleted, Status,
                                            (
                                                SELECT Id, ProcessInstanceId, ActorId, Actor.Name, StepStatus, Comments, ElapsedTimeInDays
                                                From StepsAndWorkItems
                                                Where 
                                                    StepStatus = 'Pending'  And
                                                    isDeleted = false 
                                                Order By 
                                                    Createddate Desc
                                                Limit 1
                                            )
                                        From ProcessInstance
                                        Where
                                            isDeleted = false and
                                            TargetObjectId In: adjIds and
                                            Status = 'Pending'
                                        Order By 
                                            Createddate Desc

                                ];
        if(process != null ) {
            for(ProcessInstance objProcessIns : process) {
                if(!objProcessIns.StepsAndWorkItems.isEmpty()) {
                    doStepsRejection(objProcessIns.StepsAndWorkItems[0].Id);
                }
            }
        }
    }

    //Auto Step level Rejection
    @TestVisible 
    private static void doStepsRejection(Id processStepId) {

        Approval.ProcessWorkitemRequest req;
        Approval.ProcessResult result;  

        req = new Approval.ProcessWorkitemRequest();
        req.setWorkitemId(processStepId);               
        req.setComments('Rejecting request.');
        req.setAction('Reject');
        result = Approval.process(req);

    }             

}

/*
*Test Class
*
*/

@isTest
public class AdjustmentAutoRejectTest {
  private static final String MANAGER_USER_ALIAS = 'TMU';
    private static final String STANDARD_USER_ALIAS = 'TSU';
    private static final String ENSEMBLE_ID = '12345678';
    private static final String CUID = 'AB1234';
    private static final String ADJUSTMENT_CREATED = 'Pending';
    private static final String ADJUSTMENT_APPROVED = 'Approved';
    private static final String ADJUSTMENT_REJECTED = 'Rejected';
    private static final String SYNC_STATUS_PENDING = 'Pending';
    private static final String SYNC_STATUS_ERROR = 'Error';
    private static final String BILLING_SOURCE_ENSEMBLE = 'Ensemble';
    private static final String ADJUSTMENT_MEMO = 'memo';
    private static final String ADJUSTMENT_CHARGE_ID = '942_1';
    private static final Decimal ADJUSTMENT_ORIGINAL_AMOUNT = 100;
    private static final Decimal ADJUSTMENT_AMOUNT = 10.00;
    private static final String ADJUSTMENT_REASON = 'AJ0402';
    private static final String BILL_SEQUENCE_NUMBER = 'AJ0402';
    private static final String ADJUSTMENT_CHARGE_SEQUENCE = '942';
    private static final Integer LIMIT_QUERIES_PLUS_ONE = Limits.getLimitQueries()+1;
    private static final Id RECORD_TYPE_ADJUSTMENT = RecordTypeCache.get(Schema.Dispute_Adjustment__c.SObjectType,'Adjustment').Id;


    @TestSetup static void setup() {
        User managerUser = (User)new SObjectBuilder(User.SObjectType)
                .put(User.Alias, MANAGER_USER_ALIAS)
                .create()
                .getRecord();
        User standardUser = (User)new SObjectBuilder(User.SObjectType)
                .put(User.Alias, STANDARD_USER_ALIAS)
                .put(User.ProfileId, SObjectFieldProviders.adminProfileProvider)
                .put(User.DelegatedApproverId, managerUser.Id)
                .put(User.Ensemble_Id__c, ENSEMBLE_ID)
                .put(User.CUID__c, CUID)
                .create()
                .getRecord();

        List<Account> accs = new SObjectBuilder(Account.sObjectType)
           .put(Account.OwnerId, standardUser.Id) 
           .put(Account.Name, new SObjectFieldProviders.UniqueStringProvider())
           .put(Account.AccountNumber, new SObjectFieldProviders.UniqueStringProvider()) 
           .put(Account.Billing_Source__c, BILLING_SOURCE_ENSEMBLE)
           .count(LIMIT_QUERIES_PLUS_ONE).create().getRecords();        

        //Create adjustments
        List<Dispute_Adjustment__c> adjustments = new SObjectBuilder(Dispute_Adjustment__c.SObjectType)
            .put(Dispute_Adjustment__c.Account__c, accs)
            .put(Dispute_Adjustment__c.Memo__c, ADJUSTMENT_MEMO)
            .put(Dispute_Adjustment__c.Manager_of_Pending_Approval_By__c, standardUser)
            .put(Dispute_Adjustment__c.RecordTypeId, RECORD_TYPE_ADJUSTMENT)
            .count(accs.size()).create().getRecords();
        new SObjectBuilder(Charge__c.SObjectType)
            .put(Charge__c.Dispute_Adjustment__c, adjustments)
            .put(Charge__c.Original_Amount__c, ADJUSTMENT_ORIGINAL_AMOUNT)
            .put(Charge__c.Amount__c, ADJUSTMENT_AMOUNT)
            .put(Charge__c.Adjustment_Reason__c, ADJUSTMENT_REASON)
            .put(Charge__c.Charge_Product_ID__c, ADJUSTMENT_CHARGE_ID)
            .put(Charge__c.Adjustment_Charge_Id__c, ADJUSTMENT_CHARGE_ID)
            .put(Charge__c.Bill_Sequence_Number__c, BILL_SEQUENCE_NUMBER)
            .put(Charge__c.Charge_Sequence_Number__c, ADJUSTMENT_CHARGE_SEQUENCE)
            .count(adjustments.size()).create().getRecords();


           }

     @isTest
     private static void givenRejectedAdjustments_whenStatusUpdate_byProcessBuilder(){

        User u = [SELECT Id FROM User WHERE Alias = :STANDARD_USER_ALIAS];
        List<Dispute_Adjustment__c> dispAdjs = [Select Id, RecordTypeId, Status__c FROM Dispute_Adjustment__c WHERE Status__C = :ADJUSTMENT_REJECTED];
        for(Dispute_Adjustment__c adj : dispAdjs){
            adj.Status__c = ADJUSTMENT_REJECTED;
        }

        Test.startTest();
        Test.setMock(HttpCalloutMock.class, mockResponse);
        System.runAs(u){
           // The following code runs as user 'u' 

        }
        Test.stopTest();

    }
}
  • If you don't even know how to begin, Trailhead is a more appropriate learning avenue. This site expects a good faith effort to solve the problem already be carried out. – Adrian Larson Feb 8 '18 at 14:10
  • Once you start writing a test class, you can edit this post to be more specific and it may get reopened. – Adrian Larson Feb 8 '18 at 14:11
0

Your invocable method is just a static method. You can simply call it on your test to verify that it works as intended.

// create your test data here
// and get the ids to a "recordIds" list

Test.startTest();
AdjustmentAutoReject.rejectAdjustmentRecord(recordIds);
Test.stopTest();

// do your asserts here

Note: the fact that your method is called from the process builder means that it is going to run on system mode, so you don't really need the runAs method, and can modify the class to without sharing if you are not planning on using it outside process builder. If you do plan on using it outside process builder you can create a method run by a class without sharing that runs your original method.

| improve this answer | |
  • Thanks for you prompt response.... could you please let me know what should be the input parameter ... – user53609 Feb 8 '18 at 12:51
  • If your method signature says it accepts a list of ids, then you pass a list of ids to it. – Renato Oliveira Feb 8 '18 at 12:53

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