+ Start a Discussion
Balayesu ChilakalapudiBalayesu Chilakalapudi 

How to cover try-catch block in my Test class

I had a try-catch block like this in my class,
 
try {
	    	String shref = ApexPages.currentPage().getURL();
	    	System.debug('>>> HouseholdMemberController shref='+shref);
} catch (Exception e) {
			System.debug('>>> ERROR ='+e);
}

Please help me to cover catch block.
Best Answer chosen by Balayesu Chilakalapudi
Amit Chaudhary 8Amit Chaudhary 8
There is no way to get Exeption in below code
String shref = ApexPages.currentPage().getURL();
That is why exception part is covering. If you will pass null value in shref then also excpetion will not come till you will not use shref variable.

If you want to cover catch in any cost try to update your code like below
public with sharing class HouseholdMemberController {
 
	public HouseholdMemberController() {
		
		System.debug('>>> HouseholdMemberController BEFORE shref');
		try {
	    	String shref = ApexPages.currentPage().getURL();
	    	System.debug('>>> HouseholdMemberController shref='+shref);
			if(Test.isRunningTest())
			{
				throw new Exception();
			}
			
		} catch (Exception e) {
			System.debug('>>> ERROR ='+e);
		}    		
	}    
}



 

All Answers

Amit Chaudhary 8Amit Chaudhary 8
Can you please post your full code. You just need to call method in which above try block is there.

Excpetion part will only cover when any exception will come. In Above Code look like no excpetion will come.
 
Balayesu ChilakalapudiBalayesu Chilakalapudi
This is the class
public with sharing class HouseholdMemberController {
 
	public HouseholdMemberController() {
		
		System.debug('>>> HouseholdMemberController BEFORE shref');
		try {
	    	String shref = ApexPages.currentPage().getURL();
	    	System.debug('>>> HouseholdMemberController shref='+shref);
		} catch (Exception e) {
			System.debug('>>> ERROR ='+e);
		}    		
	}    
}

My Test class is
@isTest
public class HouseholdMemberControllerTest {
    public static testmethod void test1(){
        PageReference pageref=Page.vlocity_ins__Household;
        Test.setCurrentPage(pageref);
        HouseholdMemberController cntrl=new HouseholdMemberController();
    }
    public static testmethod void test2(){  
        PageReference pageref=Page.vlocity_ins__Household;   
        pageref=null;
        Test.setCurrentPage(pageref);
        HouseholdMemberController cntrl=new HouseholdMemberController(); 
    }
}
It is not covering catch block of source code.
Amit Chaudhary 8Amit Chaudhary 8
There is no way to get Exeption in below code
String shref = ApexPages.currentPage().getURL();
That is why exception part is covering. If you will pass null value in shref then also excpetion will not come till you will not use shref variable.

If you want to cover catch in any cost try to update your code like below
public with sharing class HouseholdMemberController {
 
	public HouseholdMemberController() {
		
		System.debug('>>> HouseholdMemberController BEFORE shref');
		try {
	    	String shref = ApexPages.currentPage().getURL();
	    	System.debug('>>> HouseholdMemberController shref='+shref);
			if(Test.isRunningTest())
			{
				throw new Exception();
			}
			
		} catch (Exception e) {
			System.debug('>>> ERROR ='+e);
		}    		
	}    
}



 
This was selected as the best answer