0

I have a controller:

public class XYZcontroller{
    public object__c sr{get;set;}
    public String srid;
    public XYZcontroller(ApexPages.StandardController controller) {
        sr = (object__c) controller.getRecord();
        srid = String.valueOf(sr.Id).substring(0, 15);
        String pageMessage = ApexPages.currentPage().getParameters().get('pageMessage');
        boolean doNotDisplay;
        String referrer = ApexPages.currentPage().getHeaders().get('REFERER');
        if(referrer.contains(srid+ '/e')){
            doNotDisplay=false;
        }else{
            doNotDisplay=true;
        }
    }
}

Test class for the above:

@isTest
public class xyzControllerTest{        
    static testMethod void testMethod1(){        
        object__c sr = new object__c (Name='test');
        insert sr;
        PageReference pageRef = Page.Thepage;
        Test.setCurrentPage(pageRef);
        ApexPages.currentPage().getParameters().put('pageMessage', 'Amessage');     
        ApexPages.currentPage().getHeaders().put('REFERER', sr.id + '/e');
        ApexPages.StandardController stdcontroller = new ApexPages.StandardController(sr);

        xyzController controller = new xyzController(stdcontroller);     
    }    
}

In the above test class I get an error at line - xyzController controller = new xyzController(stdcontroller); saying:

Error Message:

System.NullPointerException: Argument 1 cannot be null Stack Trace External entry point Class.xyzControllerTest.testMethod1: line 14, column 1

I am thinking it is due to the public String srid; in the XYZcontroller which does not get a value inside the constructor. ( srid = String.valueOf(sr.Id).substring(0, 15); )

1

Since you are not passing REFERER as parameter which you are trying to access in the constructor, that's why the error is.

So, before creating instance of the stdController, assign header value.

@isTest
public class xyzControllerTest{


 static testMethod void testMethod1(){

     object__c sr = new object__c (Name='test');
     insert sr;
     PageReference pageRef = Page.Thepage;
     Test.setCurrentPage(pageRef);
     ApexPages.currentPage().getParameters().put('id',sr.Id);
     ApexPages.currentPage().getParameters().put('pageMessage', 'Amessage');  
     ApexPages.currentPage().getHeaders().put('REFERER', sr.id + '/e');  
     ApexPages.StandardController stdcontroller = new ApexPages.StandardController(sr);

     xyzController controller = new xyzController(stdcontroller);     
 }

}

Controller

Also, put a null check before using referrer

public class XYZcontroller{
public object__c sr{get;set;}
public String srid {get;set;}
public XYZcontroller(ApexPages.StandardController controller) 
{
    sr = (object__c) controller.getRecord();
    srid = String.valueOf(controller.getId()).substring(0, 15);
    String pageMessage = ApexPages.currentPage().getParameters().get('pageMessage');
    boolean doNotDisplay;

    String referrer = ApexPages.currentPage().getHeaders().get('REFERER');
    if(referrer !=null && referrer.contains(srid+ '/e'))
    {
        doNotDisplay = false;
    }else
    {
        doNotDisplay=true;
    }
}
  • He is using standard controller so its not required – Avijit Chakraborty Jun 20 '17 at 0:18
  • I got it, issue in the referrer – Santanu Boral Jun 20 '17 at 0:23
  • I do have the referrer before ApexPages.StandardController but still the same error comes up – SfdcBat Jun 20 '17 at 0:26
  • can you use my code and check if it works – Santanu Boral Jun 20 '17 at 0:29
  • Santanu , I still get the same error with your code as well. Is this because public String srid; is not set in the constructor? – SfdcBat Jun 20 '17 at 0:38
1

Below code is running with 90% coverage. Please note the changes.

@isTest
    public class xyzControllerTest{


     static testMethod void testMethod1(){

         object__c sr = new object__c (Name='test');
         insert sr;
         ApexPages.currentPage().getParameters().put('pageMessage', 'Amessage');         
         ApexPages.currentPage().getHeaders().put('REFERER', sr.id + '/e');
         ApexPages.StandardController stdcontroller = new ApexPages.StandardController(sr);
         xyzController controller = new xyzController(stdcontroller);     
     }

    }
  • This does give the error. I removed - Test.setCurrentPage(pageRef); only rest of the code I think is the same as what I have in the question. Correct? – SfdcBat Jun 20 '17 at 0:29
  • Test.setCurrentPage(pageRef) and ApexPages.currentPage().getParameters().put both not required at same time – Avijit Chakraborty Jun 20 '17 at 0:45
  • Ok! I removed them and ran the test again . It give me the same error message. Why does it says - System.NullPointerException: Argument 1 cannot be null where I create a new object of the controller? Any idea? – SfdcBat Jun 20 '17 at 0:48
  • What are the other fields in your Object. I just created Object__c without any custom field and it worked without any issue – Avijit Chakraborty Jun 20 '17 at 0:52
  • Only required field is Name, no need of custom field to create the record – SfdcBat Jun 20 '17 at 1:07

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.