0

Hi We have a apex class and trying to write Apex class for the code coverage. For some reason the Test class wont pass.

Please advise on this Iam getting Attempt to de reference error.

Please find the code below.

public class headerUtility {

    /** This is a hack to determine is the page is using SSL or not. If CipherSuite is defined that means it is HTTPS **/
    public Boolean hasSSL { get { return ApexPages.currentPage().getHeaders().get('CipherSuite') != null; } }

    /** This is used to determine the device type **/
    public String userAgent { get { return ApexPages.currentPage().getHeaders().get('USER-AGENT'); } }

    /** This will return which Salesforce Server you are on, example: na8.salesforce.com **/
    public String currentServer { get {return ApexPages.currentPage().getHeaders().get('X-Salesforce-Forwarded-To'); } }

    /** Check the useragent string and assign the device **/
    public String deviceType { get; set; } {
       if(userAgent.contains('iPhone')) deviceType = 'iPhone';
       else if(userAgent.contains('iPad')) deviceType = 'iPad';
       else if(userAgent.contains('BlackBerry')) deviceType = 'BlackBerry';
       else deviceType = 'Other';
    }

    /** You could query a custom object to determine your home page, check the device or simply hard code it here **/
    public String returnUrl {get; set; } {

        if (deviceType == 'iPad') {
            returnUrl = '/apex/LbMain';
        } else {
            returnUrl = '/apex/LbMain';

        }
    }

    public String username {get; set;}
    public String password {get; set;}

    public PageReference login(){

        String startUrl = System.currentPageReference().getParameters().get('startURL');
        return Site.login( username, password, startURL);
    }

    /** Check to see if the page is using HTTPS, if not redirect it back to itself with HTTPS **/
    public PageReference redirect() {
        if (!hasSSL) {
            string host = ApexPages.currentPage().getHeaders().get('host');
            string url = ApexPages.currentPage().getUrl();
            PageReference homePage = new PageReference('https://'+host+url);
            homePage.setRedirect(true);
            return homePage;
        } else {
            return null;
        }
    }

}

Test class:

@istest
public class HeaderUtility_Test {
    public static testmethod void TestHeader(){


        headerUtility h = new headerUtility();
        h.username = 'Test@hilton.com';
        h.password ='Test123!';

        pagereference pageref = page.LbMain;

        test.setCurrentPage(pageref);

        h.login();
        h.redirect();


    }

}

Error Message:

System.NullPointerException: Attempt to de-reference a null object
Class.autoFinance.headerUtility.: line 14, column 1
Class.autoFinance.HeaderUtility_Test.TestHeader: line 6, column 1.

Thank You

2

userAgent is empty, you need to fullfill your getters needs.

@istest
public class HeaderUtility_Test {
    @istest
    public static void TestHeader(){

        PageReference pageref = Page.LbMain;    
        pageref.getHeaders().put('USER-AGENT','iPhone');
        Test.setCurrentPage(pageref);

        HeaderUtility h = new HeaderUtility();
        h.username = 'Test@hilton.com';
        h.password ='Test123!';

        h.login();
        h.redirect();
    }
}

ps. consider adding assertions ;) Tests are not supposed to create coverage, they should test if your features work as expected..

  • this should be used in Test class to fulfill? – Samuel Robert Nov 7 '17 at 11:36
  • @SamuelRobert jep, right after you defined pageref, I edited my answer. – Basti Nov 7 '17 at 11:40
  • I have replaced it , however Iam still getting the same error – Samuel Robert Nov 7 '17 at 11:42
  • try to create your headerUtility after you initialized the page. – Basti Nov 7 '17 at 11:43
  • 1
    Great Bro I got the test run successful – Samuel Robert Nov 7 '17 at 11:53

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.