You need to sign in to do that
Don't have an account?
Test Class
hello,
how can i cover try-catch block in test class.
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;
}
}
Test Class
public class thecontrollerTests {
public static testMethod void testMyController() {
PageReference pageRef = Page.success;
Test.setCurrentPage(pageRef);
TestPagecontroller controller = new TestPagecontroller();
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 TestPagecontroller();
controller.setLastName('lastname');
controller.setFirstName('firstname');
controller.setCompany('acme');
controller.setEmail('firstlast@acme.com');
System.assertEquals('lastname', controller.getLastName());
System.assertEquals('firstname', controller.getFirstName());
System.assertEquals('acme', controller.getCompany());
System.assertEquals('firstlast@acme.com', controller.getEmail());
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);
}
}
here i am getting 92% Code Coverage. but my test class cant cover catch block.
Plz help me
You have to write a test method to throw an exception. Here is an example.
Controller
Test Class
If a reply to a post answers your question or resolves your problem, please mark it as the solution to the post so that others may benefit.
All Answers
You have to write a test method to throw an exception. Here is an example.
Controller
Test Class
If a reply to a post answers your question or resolves your problem, please mark it as the solution to the post so that others may benefit.
Here is the previous example in PDF version because code of previous post is not clear.
http://www.4shared.com/office/ziSIAJcN/Testing_Example.html
If a reply to a post answers your question or resolves your problem, please mark it as the solution to the post so that others may benefit.
thanks for reply
now my test class cover catch block