+ Start a Discussion
chiranjeevitgchiranjeevitg 

Not able to cover getter method

Hi,

 

I am trying to cover the test coverage for getter setter part, but I am unable to cover, Please suggest me to get the coverage.

 

public String strCurrentPageAlerts{
    get{         
        String temp1 = PageUrl.subString(0,12);       
        return 'https://'+BaseURL+temp1;
        }set;
    }
    public String strCurrentPage{
    get{
        String temp = PageUrl.subString(0,14);
        return 'https://'+BaseURL+temp;
        }set;
    }

 when i tried the below code I am able to cover only first lines.

 

 

MyController cntrl = new MyController();
cntrl.strCurrentPageAlerts = 'test';
cntrl.strCurrentPage = 'test1';

 

Thanks.

Best Answer chosen by Admin (Salesforce Developers) 
Navatar_DbSupNavatar_DbSup

Hi,

 

Try the below code as reference:

 

MyController cntrl = new MyController();
cntrl.strCurrentPageAlerts = 'test';
cntrl.strCurrentPage = 'test1';
String getstrCurrentPageAlerts=cntrl.strCurrentPageAlerts;

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved. 

All Answers

Navatar_DbSupNavatar_DbSup

Hi,

 

Try the below code as reference:

 

MyController cntrl = new MyController();
cntrl.strCurrentPageAlerts = 'test';
cntrl.strCurrentPage = 'test1';
String getstrCurrentPageAlerts=cntrl.strCurrentPageAlerts;

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved. 

This was selected as the best answer
chiranjeevitgchiranjeevitg

Thank u very mcuh

Karna_ShivaKarna_Shiva
I tried above example, but it is throwing error like  cntrl.strCurrentPageAlerts variable doesn't exsist.

I think this is the way to write test code for coverage for getter methods.

controller = new thecontroller();
controller.setLastName('lastname');
controller.setFirstName('firstname');
controller.setCompany('acme');
controller.setEmail('firstlast@acme.com');
controller.getLastName();
controller.getFirstName();
controller.getEmail();
controller.getCompany();
controller.getStatus();