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
Ola BamideleOla Bamidele 

How to Cover "return detailPage;" Line In My Test Class

Hi Gurus, 

I have finished writting my test class for my entension page and its curently on 92% coverage - as theres one line that is not covered.  ( line 25 -  return detailPage; )

This is my code extension apex class code: 

public with sharing class extencntrl{
    
    private ApexPages.StandardController sc;
    public extencntrl(ApexPages.StandardController sc) {
        this.sc = sc;
    }
    
    public boolean displayPopup {get; set;}     
    
    public void closePopup() {  
        displayPopup = false;
        
    }     
    public void showPopup() {        
        displayPopup = true;
        
    }
    
public PageReference Save() {
          PageReference detailPage = sc.Save();
        if (detailPage != null) {
            PageReference editPage = new PageReference(Label.websiteurl); 
            return editPage ;
        } else {
            return detailPage;
        }
    }
}




This is my test class code:


@isTest
public class extencntrlTest{
    static testmethod void validateStandardController()
    {
        Account acc = new Account();
        acc.name ='Test';
        insert acc;
        
        ApexPages.StandardController sc = new ApexPages.StandardController(acc);
        extencntrl ext = new extencntrl(sc);
        ext.closePopup();
        ext.showPopup();
        ext.Save();
    }
}



The only line that my test class does not cover is line 25 ( return detailPage;) on my extension. 

Does anyone know what I can add to my code to cover the last line aswell? If so, please let me know!

Thanks very much!
Amit Chaudhary 8Amit Chaudhary 8
Please try to update your test class like below
@isTest
public class extencntrlTest{
    static testmethod void validateStandardController()
    {
        Account acc = new Account();
        acc.name ='Test';
        insert acc;
        
        ApexPages.StandardController sc = new ApexPages.StandardController(acc);
        extencntrl ext = new extencntrl(sc);
        ext.closePopup();
        ext.showPopup();
        ext.Save();
    }
    static testmethod void validateStandardController1()
    {
        Account acc = new Account();
        //acc.name ='Test';
        //insert acc;
         
        ApexPages.StandardController sc = new ApexPages.StandardController(acc);
        extencntrl ext = new extencntrl(sc);
        ext.closePopup();
        ext.showPopup();
        ext.Save();
    }
    
}

Let us know if this will help you