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. I dont have permission to change Apex Controller so it has to do something in test class itself

public with sharing class Support_Footer_Controller {
    public List<Language_Codes__c> langCodeList = new List<Language_Codes__c>();
    public map<string, string> langCodeURLMap = new map<string, string>();
    //Default Constructor
    public Support_Footer_Controller(){
    }
    //Function to redirect contact support page
    public pagereference redirectToContactSupport(){
        try{
            //Get language code and URL parameters from custom setting
            langCodeList = Language_Codes__c.getAll().values();
            for(Language_Codes__c lang : langCodeList){
                langCodeURLMap.put(lang.language_code__c, lang.language_URL__c);
            }
            String userLanguage = ApexPages.currentPage().getParameters().get('lang');
            if(langCodeURLMap.get(userLanguage) != null){
                userLanguage = langCodeURLMap.get(userLanguage);
            }
            //Redirect to contact support page 
            pagereference pg = new pagereference(System.Label.url1+userLanguage+System.Label.url2);
            system.debug('userLanguage=='+userLanguage);
            pg.setRedirect(true);
            return pg;
        }
        catch(Exception e){
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error, e.getMessage()));
            return null;
        }

    }
}
Best Answer chosen by JAYESH RATHOR
Lalit Mistry 21Lalit Mistry 21
Hi Jayesh,

Below test method should help you. Just ensure you create custom setting records before System.Test.startTest()
private static testMethod void testForException(){
		//Your test setup method calls goes here. for e.g. creating custom settings, records, etc
		System.Test.startTest();
		Support_Footer_Controller controller = new Support_Footer_Controller();
		controller.langCodeURLMap = null;
		controller.redirectToContactSupport();
		System.Test.stopTest();
	}

 

All Answers

Lalit Mistry 21Lalit Mistry 21
Hi Jayesh,

Below test method should help you. Just ensure you create custom setting records before System.Test.startTest()
private static testMethod void testForException(){
		//Your test setup method calls goes here. for e.g. creating custom settings, records, etc
		System.Test.startTest();
		Support_Footer_Controller controller = new Support_Footer_Controller();
		controller.langCodeURLMap = null;
		controller.redirectToContactSupport();
		System.Test.stopTest();
	}

 
This was selected as the best answer
JAYESH RATHORJAYESH RATHOR
Thanku so much @Lalit Mistry 21