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
JAYESH RATHORJAYESH RATHOR 

Can any one help me out that how to cover the code coverage for below catch excpetion in test class.

public pagereference redirectLater(){
        pagereference pgRd;
        try{
            pgRd = new pagereference('/Save_Page?lang='+ApexPages.currentPage().getParameters().get('lang'));
            pgRd.setRedirect(true);
        }
        catch(Exception e){
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error, e.getMessage()));
            return null;
        }     
        return pgRd;    
    }
 
Best Answer chosen by JAYESH RATHOR
Lalit Mistry 21Lalit Mistry 21
Hi Jayesh,

Assuming your method is in class DummyClass, slightly update your class as in below code
public with sharing class DummyClass {
    @TestVisible
    private static Boolean testException = false;
    
    public pagereference redirectLater(){
        pagereference pgRd;
        try{
            pgRd = new pagereference('/Save_Page?lang='+ApexPages.currentPage().getParameters().get('lang'));
            if(testException){
                pgRd = null;
            }
            pgRd.setRedirect(true);
        }
        catch(Exception e){
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error, e.getMessage()));
            return null;
        }     
        return pgRd;    
    }
}

Then below test class should help you cover for exception block.
@IsTest
private with sharing class DummyClassTest {
    
    private static testMethod void testForException(){        
        DummyClass controller = new DummyClass();
        DummyClass.testException = true;
        System.Test.startTest();
        controller.redirectLater();
        System.Test.stopTest();
    }
}

If this solves your problem, then mark this as an answer to help others.

All Answers

Lalit Mistry 21Lalit Mistry 21
Hi Jayesh,

Assuming your method is in class DummyClass, slightly update your class as in below code
public with sharing class DummyClass {
    @TestVisible
    private static Boolean testException = false;
    
    public pagereference redirectLater(){
        pagereference pgRd;
        try{
            pgRd = new pagereference('/Save_Page?lang='+ApexPages.currentPage().getParameters().get('lang'));
            if(testException){
                pgRd = null;
            }
            pgRd.setRedirect(true);
        }
        catch(Exception e){
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error, e.getMessage()));
            return null;
        }     
        return pgRd;    
    }
}

Then below test class should help you cover for exception block.
@IsTest
private with sharing class DummyClassTest {
    
    private static testMethod void testForException(){        
        DummyClass controller = new DummyClass();
        DummyClass.testException = true;
        System.Test.startTest();
        controller.redirectLater();
        System.Test.stopTest();
    }
}

If this solves your problem, then mark this as an answer to help others.
This was selected as the best answer
JAYESH RATHORJAYESH RATHOR
thank u so much ....@Lalit Mistry 21