I am trying to cover this class but unable to cover url = '/servlet/servlet.FileDownload?file='+lstDocument[0].id; and pagereference method. Need help in increasing test coverage.


public class documentctr {

public String docName{get;set;}
public String url{get;set;}

public documentctr(){

    if(ApexPages.CurrentPage().getParameters().get('doc') != null){

        docName =  ApexPages.CurrentPage().getParameters().get('doc');
        List<Document> lstDocument = [select id from Document where DeveloperName =: docName];    
        if(lstDocument.size() > 0){
            url = '/servlet/servlet.FileDownload?file='+lstDocument[0].id;

public pagereference returnUrl(){

    if(url != null){
        pagereference page = new pagereference(url);
        return page;
    return null;


Test Class:


public class documentctrTracker{

static Folder objFolder;
public String url;

static testMethod void firstMethod() {


    documentctr ob1 = new documentctr();
    PageReference pageRef = ob1 .returnUrl();

    PageReference newPage = Page.pvdocload;
    documentctr ob = new documentctr();

static void loadData(){


static void insertFolder(){

    objFolder = [select id from Folder where Type = 'Document' limit 1];        

static void insertDocument(){  

    Document obj = new Document();
    obj.Name = 'newFile';
    obj.FolderId = objFolder.Id;
    insert obj;

   // ApexPages.currentPage().getParameters().put('doc','newFile');
    ApexPages.Standardcontroller sc = new ApexPages.Standardcontroller(obj);
    documentctr ob1 = new documentctr();

    PageReference newPage = Page.pvdocload;
    catch(DmlException e){
  • Have you tried adding `Test.setCurrentPage(ob1)'; right above the 'returnURL' call in your test class? Commented Apr 18, 2016 at 15:13
  • yes but its not working. Commented Apr 18, 2016 at 15:22
  • I posted an answer. If it still doesn't work make sure to update your sample code so others (and I) can see what your new code looks like,. Commented Apr 18, 2016 at 15:25

2 Answers 2


Do the following:

Add the line below right above the 'returnURL':

PageReference newPage = Page.pvdocload;

Change the parameter line to


In your code, change System.currentPageReference() to ApexPages.CurrentPage()

  • its throwing Error: Compile Error: Method does not exist or incorrect signature: [documentctr].getParameters() at line 42 column 9 Commented Apr 18, 2016 at 15:37
  • You're right. My Bad. Can you please share with me the name of your VF page? (The one documentctr is the controller for) Commented Apr 18, 2016 at 15:38
  • its "apex/pvdocload Commented Apr 18, 2016 at 15:41
  • Just updated my code samples Commented Apr 18, 2016 at 15:46
  • pvdocload newPage = Page.pvdocload; it will throw Compile Error: Invalid type: pvdocload Commented Apr 18, 2016 at 15:55

It seems you are searching a document by its developer name in class and no value is provided for developer name of document in test class. Therefore, Please provide the same value that you are looking for and it's better to have logged in user / running user's id as folder id of the document you are querying for. So the snippet of insert document function will look like this:-

Document recordDocument = new Document ( Name = 'newFile', DeveloperName = 'newFile', FolderId = UserInfo.getUserId() ); insert recordDocument;

You must log in to answer this question.

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