function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
留学情報館留学情報館 

Test Class For FinishURL StandartsetController - Extension

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.
 
Can you please help me with my code and test class so that I can deploy these codes into the production?

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
```

I just would love to get some direction on this and would appreciate any kind of help asap TT
 
Yogeshwar TailorYogeshwar Tailor
Hi,

Can you add the below code to your test class?
 
Test.startTest();
 
  ApexPages.StandardSetController stdSetController = new ApexPages.StandardSetController(OnlistTestList);
  stdSetController.setSelected(OnlistTestList);
  NGController ext = new NGController(stdSetController);

 PageReference pageRef = Page.AccountPlan;
 pageRef.getParameters().put('myparameter', String.valueOf(testAccountPlanInsert.Id)+’#test’);
 Test.setCurrentPage(pageRefe);
 
Test.stopTest();



Best,
Yogesh

 
留学情報館留学情報館
Hi Yogesh, thank you so much for your quick answer! i'll try the codes and let you know about the result asap!
Thanks again^^