not sure what I am doing wrong here with setting the page id in the test class. Can anyone see what I am doing wrong? The failed test is throwing System.NullPointerException: Argument 1 cannot be null on line ecpc.creditPullandReRender();

public static testMethod void successtestLeadCreditPullandReRender(){
    //inserting the data
    EquifaxCreditPull__c post = new EquifaxCreditPull__c(APIKey__c = '132', Endpoint__c = 'test', Token__c = 'test');
    insert post;
    Lead l = new Lead(FirstName = 'Test', LastName = 'Test2', Social_Security_Number__c = '000000000', SSN_Encrypted__c = '000000000', Company = 'TestCo');
    insert l;
    Consumer_Credit_Summary__c ccs = new Consumer_Credit_Summary__c(FICO__c = 123, Transaction_Date__c = Date.today());
    insert ccs;

    //creating the mock callout
    SingleRequestMockClass fakeResponse = new SingleRequestMockClass(200,'Complete','{"ResultStatus":"Success"}',null);

    //setting the page id
    PageReference pageRef = Page.EquifaxCreditPullAccountPage;
    pageRef.getParameters().put('id', String.valueOf(l.Id));

    //calling the controller and passing it the lead as the standard object
    EquifaxCreditPullController ecpc = new EquifaxCreditPullController(new Apexpages.StandardController(l));

    Test.setMock(HttpCalloutMock.class, fakeResponse);
    System.assertNotEquals(null, fakeResponse,'{"ResultStatus":"Success"}');

public void creditPullandReRender(){
    String currentPageId = ApexPages.currentPage().getParameters().get('id');
    if(currentPageId.startsWith('001')) { //account
        string selectedContactID = paramValue;
        System.debug('selectedContactID :'+ selectedContactID);
        pullCred(null, selectedContactID, null);
    else if(currentPageId.startsWith('00Q')) { //lead
        pullCred(currentPageId, null, null);
    else if(currentPageId.startsWith('a1jm')) { //credit review
        pullCred(null, null, currentPageId);

I can see with debugs that the page is being set correctly in the test. Why would the page ID not carry over to the method?


A couple notes:

  • Don't ever include system.assert(true) in your tests (nor any other assertion which cannot fail). It reads as an attempt to circumvent static analysis scans of your code quality.
  • There is no need to call Test.setCurrentPage or construct a PageReference. You don't care what the current page is, just its parameters. So use:

    ApexPages.currentPage().getParameters().put('id', value);
  • You should not check the SObjectType of your Id using string prefix values. Instead, cast it to an Id proper and call the getSObjectType method:

    Id currentPageId = ApexPages.currentPage().getParameters().get('id');
    SObjectType currentPageType = (currentPageId == null) ?
        null : currentPageId.getSObjectType();
    if (currentPageType == Account.sObjectType)
        // do stuff
    // etc

    This approach has numerous advantages, including but not limited to a hard reference to that sObjectType, allowing various parts of the system to understand that your code is dependent on that object's existence. It also makes your code more human readable without comments.

As for your specific error, it seems like the pullCred method may not accept one or all of the null parameters you pass in. Further investigative steps include:

  • pulling the complete stack trace string
  • checking how you use the parameters in your pullCred method and if you should allow null inputs
  • all great advice. thank you. I ended up finding that it was the custom setting that was causing an issue. Once I updated it in the test class everything was passing. so for those who are curious, the test was written correctly it was a custom setting declaration issue. – Olivia Jun 27 '18 at 22:52

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.