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
Anish Mahadik 1Anish Mahadik 1 

I am unable to get code coverage for Cancel button(which resets the entire page).

Hi all,

I am unable to create the test class for cancel button. I tried but got error 
 Argument 1 cannot be null.
Please provide the test class for following code

   public pagereference cancel()
    {
     
       PageReference reqPageRef = new PageReference(ApexPages.currentPage().getParameters().get('retURL')); 
       reqPageRef.setRedirect(true);  
       return reqPageRef;
    }

Thanks
Best Answer chosen by Anish Mahadik 1
Niraj Kr SinghNiraj Kr Singh
Hi Anish,

You can try this :
Note: you should update here accordingly
@isTest private class TestClass {
    @isTest static void test1() {
        //VF Page is being referenced here and url is passed
        PageReference pageRef = Page.<You Page Name Here>;
        pageRef.getParameters().put('retURL','https://login.salesforce.com/');
        Test.setCurrentPageReference(pageRef);
        Test.startTest();
            <Your Class Name> objClass = new <Your Class Name>();
            objClass.cancel();
        Test.stopTest();
    }
}

Thanks
Niraj

All Answers

Raj VakatiRaj Vakati
YOU Can call this method in test class as below

<YOUR_CONTROLLER_INSTANCE_REF>.cancel();

like 
MyController myCon = new MyController();
myCon.cancel();
Niraj Kr SinghNiraj Kr Singh
Hi Anish,

You can try this :
Note: you should update here accordingly
@isTest private class TestClass {
    @isTest static void test1() {
        //VF Page is being referenced here and url is passed
        PageReference pageRef = Page.<You Page Name Here>;
        pageRef.getParameters().put('retURL','https://login.salesforce.com/');
        Test.setCurrentPageReference(pageRef);
        Test.startTest();
            <Your Class Name> objClass = new <Your Class Name>();
            objClass.cancel();
        Test.stopTest();
    }
}

Thanks
Niraj
This was selected as the best answer