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
lvivaninlvivanin 

Testing custom controllers;

I trying to test the controller already in the documentation (

Testing Custom Controllers and Controller Extensionshttp://www.salesforce.com/us/developer/docs/pages/index.htm

 

).
But it doesn't  go beyound 71%. It is not hitting the get methods.

Any idea to achieve the coverage to 100%

 

__________________________________________________________


public class thecontroller {

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

public thecontroller() {
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;
}
}

public class thecontrollerTests {

public static testMethod void testMyController() {
PageReference pageRef = Page.success;
Test.setCurrentPage(pageRef);

thecontroller controller = new thecontroller();
String nextPage = controller.save().getUrl();

// Verify that page fails without parameters
System.assertEquals('/apex/failure?error=noParam', nextPage);

// Add parameters to page URL
ApexPages.currentPage().getParameters().put('qp', 'yyyy');

// Instantiate a new controller with all parameters in the page
controller = new thecontroller();
controller.setLastName('lastname');
controller.setFirstName('firstname');
controller.setCompany('acme');
controller.setEmail('firstlast@acme.com');
nextPage = controller.save().getUrl();

// Verify that the success page displays
System.assertEquals('/apex/success', nextPage);
Lead[] leads = [select id, email from lead where Company = 'acme'];
System.assertEquals('firstlast@acme.com', leads[0].email);
}
}
<apex:page controller="thecontroller" tabstyle="lead">
<apex:pageBlock>
<apex:form>
<h1>Test page for adding leads</h1>
<p>This is a test page for adding leads.</p>
<p>First name: <apex:inputText value="{!FirstName}"></apex:inputText></p>
<p>Last name: <apex:inputText value="{!LastName}"></apex:inputText></p>
<p>Company: <apex:inputText value="{!Company}"></apex:inputText></p>
<p>Email address: <apex:inputText value="{!Email}"></apex:inputText></p>
<apex:commandButton action="{!save}" value="Save New Lead"/>
</apex:form>
</apex:pageBlock>
</apex:page>

___________________________________________________________
Best Answer chosen by Admin (Salesforce Developers) 
Richie DRichie D

You already have the setMethod calls, you just need to add the get ones!

e.g.

 

//already existscontroller.setLastName('lastname');//test the getSystem.AssertEquals('lastname',controller.getLastName());

 

 Do this for each of your set methods and you should be able to get nearer 100%...

 

All Answers

Richie DRichie D

You already have the setMethod calls, you just need to add the get ones!

e.g.

 

//already existscontroller.setLastName('lastname');//test the getSystem.AssertEquals('lastname',controller.getLastName());

 

 Do this for each of your set methods and you should be able to get nearer 100%...

 

This was selected as the best answer
lvivaninlvivanin

Thank you Richie D!!

I am now getting 92%

TehNrdTehNrd

One cool tip.

 

This:

 

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

 Can be consolidated in to this:

 

public String firstName {get; set;}