0

Hi I have created an apex class as seen below for an Apex Visualforce page. But I am having a hard time creating the test class for the code. I would appreciate any help.

public class CC2TemplateController {
    public String pref = 'yahoo.com';
    PageReference pr;
    public String globalNav {get;set;}
    public String navmobileRadioButtons {get;set;}
    public String navmobile {get;set;}
    public String logo {get;set;}
    public String nav {get;set;}
    public String noscript {get;set;}
    public String backToTop {get;set;}
    public String footer {get;set;}

    public CC2TemplateController() {
        if (String.isNotBlank(Label.CC2WarrantyTemplateUrl)) {
            pref = Label.CC2WarrantyTemplateUrl;
        }
        string temp = 'http://' + pref + '/includes/globalnav.html';
        system.debug(':::PageRference globalnav:::' + temp);

        pr = new PageReference(temp);
        globalNav = pr.getContent().toString();

        pr = new PageReference('http://' + pref + '/includes/navmobile-radio-buttons.html');
        navmobileRadioButtons = pr.getContent().toString();

        pr = new PageReference('http://' + pref + '/includes/navmobile.html');
        navmobile = pr.getContent().toString();

        pr = new PageReference('http://' + pref + '/includes/logo.html');
        logo = pr.getContent().toString();

        pr = new PageReference('http://' + pref + '/includes/nav.html');
        nav = pr.getContent().toString();

        pr = new PageReference('http://' + pref + '/includes/noscript.html');
        noscript = pr.getContent().toString();

        pr = new PageReference('http://' + pref + '/includes/back-to-top.html');
        backToTop = pr.getContent().toString();

        pr = new PageReference('http://' + pref + '/includes/footer.html');
        footer = pr.getContent().toString();
    }
}
1
1

After seeing your code, it seems that for different url, it set value to different variable. For this you may create test methods In this method you use:

  1. Test.setCurrentPageReference() method to set current page url.
  2. call ApexPages.StandardController() to call controller, and then internally your constructor call.
  3. Now you verify the value of your variable. i.e. it's value is equal to value that set in Constructor.

Hope it helps:)

1

The best solution, I found for the creating test class follow the link:

Test method to support getContent call

or if you want to create a test class for code coverage you have to replace the code pr.getContent().toString(); with (!Test.isRunningTest() ? pr.getContent().toString() : '');

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.