0

I need test class for the method below:

This class is written for overriding the view..

public PageReference redirectGroup()
{
    if(Url.getSalesforceBaseUrl().toExternalForm()  == 'https://hello.cs20.force.com') // Checks for Community URL...
    {
        Id groupId = ApexPages.currentPage().getParameters().get('id');
        Id feedId = ApexPages.currentPage().getParameters().get('fid');
        if(feedId != null)
        {
            PageReference pg = new PageReference('/Community_FeedDetail');
            pg.getParameters().put('Id', groupId);
            pg.getParameters().put('fid', feedId);
            return pg;
        }
        else
        {
            PageReference pg = new PageReference('/Community_Groups_Feed');
            pg.getParameters().put('Id',groupId );
            return pg;
        }   
    }
    else
    {
       PageReference pg = new PageReference('/'+ApexPages.currentPage().getParameters().get('id')+'?nooverride=1');
       pg.setRedirect(true);
       return pg;
    }

}

Test class:

@isTest
private class ClearSightGroupRedirectExtensionTest{

    public ApexPages.StandardController stdController;
    public ClearSightGroupRedirectExtensionTest(ApexPages.StandardController con){
              stdController = con;
    }

    public static testMethod void testRedirect() {
         Contact c = new Contact();
         c.FirstName = 'Joe';
         c.LastName = 'Schmoe';
         Insert c;
         Test.startTest();
         ApexPAges.StandardController sc = new ApexPages.StandardController(c);
         ClearSightGroupRedirectExtension redirect = new ClearSightGroupRedirectExtension(sc);  
         PageReference pageRef = Page.Community_Groups_Feed;    
         Test.setCurrentPage(pageRef);
         redirect.redirectGroup();
    }
}

I get 45% coverage with that code, but need to check the condition for community url and want coverage with that block

4
  • 1
    What have you tried till now? Pls dont expect people to write the code for you. We can help if you have any difficulties but you need to let us where you are having problems – Prady Jul 21 '15 at 10:58
  • Test.startTest(); ApexPAges.StandardController sc = new ApexPages.StandardController(c); ClearSightGroupRedirectExtension redirect = new ClearSightGroupRedirectExtension(sc); PageReference pageRef = Page.Hydra_Community_Groups_Feed; Test.setCurrentPage(pageRef); redirect.redirectGroup(); – Vetriselvan Manoharan Jul 21 '15 at 11:01
  • I got 45% coverage with this.. But I need a way to check if it community URL and need to cover the codes inside the if condition. – Vetriselvan Manoharan Jul 21 '15 at 11:01
  • Kindly edit your question and add the test class code. its easier to read that way – Prady Jul 21 '15 at 11:01
1

Rather than setting the community URL directly in code (and to allow tests to pass), what I have done in the past

  1. Create a custom setting to hold the URL string for the community
  2. Check the salesforceBaseURL against the value in the custom setting.

Then in your test method you can:

  1. Insert a custom setting value to the Url.getSalesforceBaseUrl().toExternalForm()

The downside is that you are going to have to use another custom setting value to flag it to test the negative use case or use a static variable to do the same.

Upside, you do not have to hard code the URL and can change it as needed without breaking your test or code.

0

Can you put Community URL in any class level public variable and then compare in your if statement. This will help you setting community URL from test class and you can move inside the if block.

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.