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
asd@as.asdasd@as.asd 

{Get; Set;} test method

hello

   i write a test method for a controller.but i get only  71% code coverage.

  i am facing  problem to write the test method for get set method.please help me

 here is my code :

  Controller: 

 

public class TestPagecontroller {

private String firstName;
private String lastName;
private String company;
private String email;
private String qp;

public TestPagecontroller () {
this.qp = ApexPages.currentPage().getParameters().get('qp');
}

public String getFirstName() {
return this.firstName;
}

public void setFirstName(String firstName) {
this.firstName = firstName;
}

public String getLastName() {
return this.lastName;
}

public void setLastName(String lastName) {
this.lastName = lastName;
}

public String getCompany() {
return this.company;
}

public void setCompany(String company) {
this.company = company;
}

public String getEmail() {
return this.email;
}

public void setEmail(String email) {
this.email = email;
}

public PageReference save() {
PageReference p = null;

if (this.qp == null || !'yyyy'.equals(this.qp)) {
p = Page.failure;
p.getParameters().put('error', 'noParam');
} else {
try {
Lead newlead = new Lead(LastName=this.lastName,
FirstName=this.firstName,
Company=this.company,
Email=this.email);
insert newlead;
} catch (Exception e) {
p = Page.failure;
p.getParameters().put('error', 'noInsert');
}
}

if (p == null) {
p = Page.success;
}

p.setRedirect(true);
return p;
}
}

 

testclass:

 

 

public class TestPagecontroller {

private String firstName;
private String lastName;
private String company;
private String email;
private String qp;

public TestPagecontroller () {
this.qp = ApexPages.currentPage().getParameters().get('qp');
}

public String getFirstName() {
return this.firstName;
}

public void setFirstName(String firstName) {
this.firstName = firstName;
}

public String getLastName() {
return this.lastName;
}

public void setLastName(String lastName) {
this.lastName = lastName;
}

public String getCompany() {
return this.company;
}

public void setCompany(String company) {
this.company = company;
}

public String getEmail() {
return this.email;
}

public void setEmail(String email) {
this.email = email;
}

public PageReference save() {
PageReference p = null;

if (this.qp == null || !'yyyy'.equals(this.qp)) {
p = Page.failure;
p.getParameters().put('error', 'noParam');
} else {
try {
Lead newlead = new Lead(LastName=this.lastName,
FirstName=this.firstName,
Company=this.company,
Email=this.email);
insert newlead;
} catch (Exception e) {
p = Page.failure;
p.getParameters().put('error', 'noInsert');
}
}

if (p == null) {
p = Page.success;
}

p.setRedirect(true);
return p;
}
}


Best Answer chosen by Admin (Salesforce Developers) 
bob_buzzardbob_buzzard

By the looks of it you are referring to automatic properties.  The only thing is, I can't see that you have the {get; set;} syntax in the code posted.

 

Anyhow, you test these simply by accessing the variable or assigning a value to it.

 

E.g.

 

public String myStr {get; set;}

 

Test:

myStr='test';
System.assertEquals(myStr, 'test');

 

All Answers

bob_buzzardbob_buzzard

By the looks of it you are referring to automatic properties.  The only thing is, I can't see that you have the {get; set;} syntax in the code posted.

 

Anyhow, you test these simply by accessing the variable or assigning a value to it.

 

E.g.

 

public String myStr {get; set;}

 

Test:

myStr='test';
System.assertEquals(myStr, 'test');

 

This was selected as the best answer
SoundarSoundar
 Hi bob_buzzard,

How can i cover a boolea in tets class.

Public Boolean isShowOpp {get;set;}    // How can i cover this line in test class.


Thanks in advance,,

regards,
Soundar Raj
Ajaysingh thakurAjaysingh thakur
controller c= new controller();    [controller is your class name]
c.isShowOpp =true;                   [any value true or false]
Hajie CarpioHajie Carpio
Hi,

I have a class defined:
public with sharing class ScheduleSlotControllerApx

with a property
public string DPSURL{get; set;}

in the visual force page it is mapped to 
<apex:form id="cciForm0"> 
<apex:inputText label="DSP URL" value="{!DPSURL}" id="DPSURL"/>
</apex:form>

then i have a button that calls a FetchScheduleSlotInfo method, in another form
<apex:form id="cciForm">        
         <!-- The rerender attribute causes addressList to be re-rendered when the button is clicked -->
         <apex:commandButton action="{!ViewScheduleSlotInfo}" value="Load Schedule Slots!" rerender="cciForm" status="callZipAPIStatus">         
         </apex:commandButton>
</apex:form>

in the code i have line of code 
System.debug('setPropertyValues URL - ' + this.DPSURL); 

to see if i'm able to get the updated value of the property.