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
Ravi kumar 292Ravi kumar 292 

How to cover the Try Block in Test Class

Hi All,

I want to cover the try block in test class. How to do this.
Below is my code.

Controller:

public with sharing class Controller { 
    Contact con;
    String rsession;
    String username;
    String password;
    String app='10003600';
    String ConId;
    String memSiteAccId;
    String  token;
    
    YodleeAPI y = new YodleeAPI();
    public YodleeFastLinkAccessController(ApexPages.StandardController controller) {
        try{
            ConId = ApexPages.CurrentPage().getparameters().get('id'); 
            con = [Select id, FirstName, LastName, PAN_ID__c from Contact where id=:ConId];
        }catch(Exception e){
            con = new Contact();
        }    
    }
    public String getRsession() {
        if(runningInASandbox()){
            // Sandbox
            username = username;
            password = password;
        }else{
            //Production
            username = username;
            password = password;          
        } 
        String cobSessionToken = y.cobLogin(username, password);
        try{
            rsession = y.Login(con.id,con.PAN_ID__c);        
            memSiteAccId = y.getAllSiteAccounts();
        }catch(exception e){}

        if(memSiteAccId!=null){
            String itemAccountId = y.getItemSummariesForSite(memSiteAccId);
            y.executeUserSearchRequest(cobSessionToken,rsession,'All','20','1','500','1','1','DataSearchService','INR',system.today().addMonths(-6).format(),system.today().format(),'ALL_TRANSACTION','True',itemAccountId);      
        }
        return rsession;
    }
    public String getToken() {
        try{
            token = y.GetToken(app);
        }catch(exception e){}
        return token;
    }
    public static Boolean runningInASandbox() {
        return [SELECT Id, IsSandbox FROM Organization LIMIT 1].IsSandbox;  
    }   
}

Test Class:

@isTest
public class YodleeAPI_Test{
     Public static testMethod void testcase(){
         Contact con = new Contact();
         con.LastName = 'ContactTest';
         con.PAN_ID__c = 'YTXPU1874H';
        // insert con;
         String conid = '003p0000006fMjCAAU';
         system.debug(conid);
         ApexPages.CurrentPage().getParameters().put('id',conid);
         ApexPages.Standardcontroller sc = new ApexPages.Standardcontroller(con);
         YodleeFastLinkAccessController yc= new YodleeFastLinkAccessController(sc);
         yc.getRsession();
         yc.getToken();
     }
}

In the above code Bold text is not covering in the test class..

Please help on this.. 
Pankaj_GanwaniPankaj_Ganwani
Hi,

To do so, give a value or condition within your try block such that it will throw an exception. If you are still not able to cover it in your test class, then you can use something like below:
 
try{
            rsession = y.Login(con.id,con.PAN_ID__c);        
            memSiteAccId = y.getAllSiteAccounts();  
            if(Test.isRunningTest())
                     Integer i = 10/0;
        }catch(exception e){}
        if(memSiteAccId!=null){
            String itemAccountId = y.getItemSummariesForSite(memSiteAccId);

 
Ravi kumar 292Ravi kumar 292
Hi Pankaj,

I tried with the same.. but its not covering the try catch block..

Please help.
Ravi kumar 292Ravi kumar 292
Hi Pankaj,

I removed the try and catch block and tried the same. Even at this situation also its not covering the rsession = y.Login(con.id,con.PAN_ID__c);.. What would be the reason.

Please let me know.
Pankaj_GanwaniPankaj_Ganwani
Just put debug statements above and below of the following code statement and see what comes:

system.debug('=====================');
String cobSessionToken = y.cobLogin(username, password);
system.debug('======cobSessionToken======'+cobSessionToken);
Ravi kumar 292Ravi kumar 292
Hi,

I am not getting any values in the debug log. Here, cobLogin method has json parsing to get the cobsessiontoken. So based on the cobsessionToken login will be done in the next step. 

Json Parsing is not covering the unit test.

Below is my json code.
JSONParser parser = JSON.createParser(res.getBody());
        while(parser.nextToken()!=null){
            if((parser.getCurrentToken()==JSONToken.FIELD_NAME) && parser.getText()=='sessionToken'){
                parser.nextToken();
                cobSessionToken = parser.getText();
                system.debug('cobSessionToken---'+cobSessionToken );
            }
        }

How to pass this.