0

I have an APEX class that ends with a finishURL method; this class is the controller-extension of a VF page.

What they all do is to collect the IDs of the selected items on a related list view, then pass them into a screen flow, and return back to the start point(list view in our case) when the flow is finished.

I am a total noob at writing code and did not have any test class for this controller at first, therefore when I tried to deploy this code into the production, I got an error on the overall code coverage.

So, I tried to write a test class but this time since I was unable to cover the finishURL code I got an error while deploying it.

How do I cover this code?

My VF page;

<apex:page standardController="OnList__c" recordSetVar="OnlistItem" extensions="NGController" tabstyle="OnList__c" lightningStylesheets="true">
    <apex:outputPanel >
        <p>{!finishURL}</p>
    </apex:outputPanel>

    <flow:interview name="NGFlow" finishLocation="{!URLFOR(finishURL)}">

        <apex:param name="NGRecordIDs" value="{!SelectedOnlistIDs}" />
    </flow:interview>

</apex:page>

My Controller;

public class  NGController {

    public String[] SelectedOnlistIDs{get;set;}
    public String finishURL{get;set;}


    public NGController(ApexPages.StandardSetController listcontroller){
        SelectedOnlistIDs = new string[]{};
        for(OnList__c OnlistItem : (OnList__c[])listcontroller.getSelected()){
            SelectedOnlistIDs.add(OnlistItem.Id);
        }
        finishURL = ApexPages.currentPage().getParameters().get('myparameter').substringBefore('#');
    }
}

The templated Test Class I tried to write;

@isTest 
public class NGControllerTest 
{
 static testMethod void OnlistDataTest() 
 {
 List <OnList__c> OnlistTestList = new List<OnList__c>();

 OnList__c OnlistTestRecord = new OnList__c();
 OnlistTestRecord.NGReason__c='Test Onlist' ;
 OnlistTestList.add(OnlistTestRecord);
 OnList__c OnlistTestRecord1 = new OnList__c();
 OnlistTestRecord1.NGReason__c='Test Onlist1' ;
 OnlistTestList.add(OnlistTestRecord1);

 insert  OnlistTestList;

 Test.startTest();
  Test.setCurrentPage(Page.NGPage);
  ApexPages.StandardSetController stdSetController = new ApexPages.StandardSetController(OnlistTestList);
  stdSetController.setSelected(OnlistTestList);
  NGController ext = new NGController(stdSetController);
 Test.stopTest();
 }
}

Here is the error I get when I run this test and also try to deploy these test classes;

System.NullPointerException: Attempt to de-reference a null object
Class.NGController.<init>: line 12, column 1
Class.NGControllerTest.OnlistDataTest: line 21, column 1

1 Answer 1

2

Your attempt to use page parameters will fail, because ApexPages.currentPage().getParameters() doesn't contain the key 'myparameter'. You need to put some value in this map key, and also make some assertions. Include something like below in your test before constructing your extension:

String expectedFinishUrl = 'abcde';
String parameter = expectedFinishURL + '#4321';
ApexPages.currentPage().getParameters().put('myParameter', parameter');

Somewhere at the end of your test, assert it got set.

system.assertEquals(expectedFinishUrl, myExtension.finishUrl, 'Always include an informative message');
3
  • Hello Adrian, thank you so much for your answer! Commented Nov 4, 2019 at 9:03
  • I have tried adding the lines you shared and I am getting the error variable finishURL does not exist at the system.assertEquals(expectedFinishUrl, NGController.finishUrl, 'Always include an informative message'); line. What is it that I am doing wrong you think? Commented Nov 4, 2019 at 9:09
  • I also tried system.assertEquals(expectedFinishUrl, stdSetController.finishUrl, 'Always include an informative message'); bu I am getting the same error. >< Commented Nov 4, 2019 at 9:12

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .