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 

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

Best Answer chosen by Admin (Salesforce Developers) 
Chamil MadusankaChamil Madusanka

You have to write a test method to throw an exception. Here is an example.

 

Controller

public class thecontroller
{
public String firstName{get;set;}
public String lastName{get;set;}
public String company{get;set;}
public String email{get;set;}
public String qp{get;set;}
public thecontroller()
{
this.qp = ApexPages.currentPage().getParameters().get('qp');
}
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 testMyControllerSuccess()
{
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);
}
public static testMethod void testMyControllerFail() { PageReference pageRef = Page.success; Test.setCurrentPage(pageRef); thecontroller controller = new thecontroller(); // 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'; //Let’s assume that email is required. We don’t assign a email //controller.setEmail='firstlast@acme.com' nextPage = controller.save().getUrl(); // Verify that the failure page displays. This COVER THE CATCH BLOCK System.assertEquals('/apex/failure?error=noInsert’, nextPage); }
}

 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

Chamil MadusankaChamil Madusanka

You have to write a test method to throw an exception. Here is an example.

 

Controller

public class thecontroller
{
public String firstName{get;set;}
public String lastName{get;set;}
public String company{get;set;}
public String email{get;set;}
public String qp{get;set;}
public thecontroller()
{
this.qp = ApexPages.currentPage().getParameters().get('qp');
}
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 testMyControllerSuccess()
{
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);
}
public static testMethod void testMyControllerFail() { PageReference pageRef = Page.success; Test.setCurrentPage(pageRef); thecontroller controller = new thecontroller(); // 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'; //Let’s assume that email is required. We don’t assign a email //controller.setEmail='firstlast@acme.com' nextPage = controller.save().getUrl(); // Verify that the failure page displays. This COVER THE CATCH BLOCK System.assertEquals('/apex/failure?error=noInsert’, nextPage); }
}

 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.

This was selected as the best answer
Chamil MadusankaChamil Madusanka

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.

asd@as.asdasd@as.asd

thanks for reply

now my test class cover catch block