Skip to main content
3 of 4
added 205 characters in body

Need Help in test class coverage its getting 71%

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.

Class:

public class documentctr {
    
    public String docName{get;set;}
    public String url{get;set;}
    
    public documentctr(){
        
        if(System.currentPageReference().getParameters().get('doc') != null){
        
            docName = System.currentPageReference().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);
            page.setRedirect(true);
            return page;
        }
        return null;
    }
}

Test Class:

@isTest

public class documentctrTracker{

static Folder objFolder;
public String url;

static testMethod void firstMethod() {
    
    loadData();
    
          
    documentctr ob1 = new documentctr();
    PageReference pageRef = ob1 .returnUrl();
    //ob1.returnUrl();
    
    PageReference newPage = Page.pvdocload;
    Test.setCurrentPage(newPage);
    newPage.getParameters().put('doc','newFile');
    //ApexPages.currentPage().getParameters().put('doc','newFile');
    documentctr ob = new documentctr();
    ob.returnUrl();
}

static void loadData(){

    insertFolder();
    insertDocument();
}

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();
    
    try{
    PageReference newPage = Page.pvdocload;
    Test.setCurrentPage(newPage);
    newPage.getParameters().put('doc','newFile');
    ob1.returnUrl();
    }
    catch(DmlException e){
    }
}