0

Hii I have created a vf page as the home page of my community.

Whenever a user goes to the community URL ,he will be redirected to the "HomePage"(CustomVf page) if he is not logged in

If he he already logged in and then he goes to that Community URL he will be redirected to a different page.

So far I am able to achieve the functionality by using session id as a reference

But while writing the test class for it I am facing the issue that how to cover the case for which the session id is null .If the user is already logged in, the session id will never be null

I am pasting my code here .Please guide me

Vf page:

<apex:page id="CommunityHome" controller="CommunityHomeController" action="{!setRedirect}" >
</apex:page>

Apex Class:

Public with sharing class CommunityHomeController {

Public CommunityHomeController(){

}

Public PageReference setRedirect(){

    String sessionId=UserInfo.getSessionId();
    system.debug('sessionId::::'+sessionId);

    If(sessionId==null){
        PageReference pageReference =new PageReference('/HomePage');
        return pageReference;
    }

    else{
        PageReference pageReference =new PageReference('/CommunityCustomLanding');
        return pageReference;
    }
}
}

Apex Test Class:

@isTest(seeAllData=False)
public class CommunityHomeController_Test {

static testMethod void CommunityHomeControllerTest(){
    test.startTest();
    CommunityHomeController communityHomeController = new CommunityHomeController();
    communityHomeController.setRedirect();
    test.stopTest();
}
}
1

There is crude way to achieve this. pls See below code.

public with sharing class CommunityHomeController {

public static boolean isApexTestForSessionId = false;

public CommunityHomeController(){

}

public PageReference setRedirect(){

    String sessionId=UserInfo.getSessionId();
    system.debug('sessionId::::'+sessionId);

    if(isApexTestForSessionId){
        sessionId = null;
    }

    if(sessionId==null){
        PageReference pageReference =new PageReference('/HomePage');
        return pageReference;
    }
    else{
        PageReference pageReference =new PageReference('/CommunityCustomLanding');
        return pageReference;
    }
}

}

Your test class can be like below.


@isTest(seeAllData=False)
public class CommunityHomeController_Test{
  static testMethod void CommunityHomeControllerTest(){
    test.startTest();
    CommunityHomeController.isApexTestForSessionId = true;
    CommunityHomeController communityHomeController = new CommunityHomeController();
    PageReference pageRef = communityHomeController.setRedirect();
    CommunityHomeController.isApexTestForSessionId = false;
    test.stopTest();
    System.assertEquals('/apex/HomePage',pageRef.getUrl());
  }
}

  • Though it is a crude way but I was able to achieve my requirement and also all the functionalities are working perfectly.Thank you Purushottam – Saswata Das May 30 '17 at 13:51
0

what you can try, you can write in one single statement using ternary operator.

If(sessionId == null) ? return new PageReference('/HomePage') : return new PageReference('/CommunityCustomLanding');

It will be covered for sure if any of the condition will be true.

  • Hi Nitish, I amgetting an error: "unexpected token:? " Isn't there any other way to achieve the desired coverage – Saswata Das May 30 '17 at 10:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.