I am struggling hard to get code coverage for following constructor method with some URL method usage. Basically the filter name I am trying to fetch is the list view ID (also need some insights to how to dynamically fetch list view id in test class for coverage)

Here is my constructor method:

public constructormethod(ApexPages.StandardSetController controller)
    this.setCon = controller;
    objectName = setCon.getRecord().getSObjectType().getDescribe().getName();

    sUrl = Apexpages.currentPage().getUrl();

    if(sUrl.contains('filterName')){  **// On running test class is failing with null exception at this line** 
        filterId = sUrl.substringAfter('%3FfilterName%3D').left(18);

test class I did so far:

 public class test_constructormethod{ 
      List<Contact>  testContacts = tdp.listContacts();
      /** test contacts add logic

      insert testContacts;

     PageReference pageRef = Page.YOUR_VF_PAGE; 
     ApexPages.StandardSetController stdctrl = new ApexPages.StandardSetController(testContacts);

1 Answer 1


You are trying to get current page information through ApexPages.currentPage(), but you are not setting/specifying the page itself.

Try to set it before calling the controller using this:


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.