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
sf.dev.ax1103sf.dev.ax1103 

test class coverage

Hi All,

 

         My class is not getting covered in below red lines.Any help in fixing in test class

 

public class Web2LeadExtension {

 
  
    public Lead wlead{
    get {
      if (wlead== null)
        wlead= new Lead ();
        return wlead;
    }
    set;
  }
  
    public Web2LeadExtension(ApexPages.StandardController
                                stdController) {
       wlead = (Lead)stdController.getRecord();
     }

     public PageReference saveLead() {
       try {
       insert(wlead);
       }
       catch(System.DMLException e) {
           ApexPages.addMessages(e);
           return null;
       }
       PageReference p = Page.mypage;
       return p;
     }
}

 

 

 

@isTest private class testLeadClass{


static testmethod void saveLeadTest(){
        
     
         Lead l = new Lead(company='test company',lastname='xyz');
         test.starttest();

 


         Web2LeadExtension ext = new Web2LeadExtension(new ApexPages.StandardController(l));

 

PageReference p = ext.saveLead();
         test.stoptest();

 

system.assertEquals(Page.mypage.getUrl(),p.getUrl());
        
      }

}

Thanks

Best Answer chosen by Admin (Salesforce Developers) 
joshbirkjoshbirk

My bad, needs to be at least:

 

Web2LeadExtension ext = new Web2LeadExtension(new ApexPages.StandardController(new Lead()));

 

Although as you have it modified should be fine.  I think the use case you've got in your extension is a bit broad, checking for wlead shouldn't be necessary as you can't get a null from the StandardController's getRecord.  It's either the record defined by the Id or a new Lead() by nature.

 

Also, DMLException may be to specific in the catch.  Try the extension like this:

 

public class Web2LeadExtension {

    public Lead wlead {get; set;}
  
    public Web2LeadExtension(ApexPages.StandardController
                                stdController) {
       wlead = (Lead)stdController.getRecord();
     }

     public PageReference saveLead() {
       try {
           insert(wlead);
       }
       catch(System.Exception e) {
           ApexPages.addMessages(e);
           return null;
       }
       PageReference p = Page.mypage;
       return p;
     }
}

 

See if that helps.

All Answers

joshbirkjoshbirk

Try another test method where:

 

Web2LeadExtension ext = new Web2LeadExtension(new ApexPages.StandardController(l));

 Lacks the incoming sObject, so:

 

Web2LeadExtension ext = new Web2LeadExtension(new ApexPages.StandardController());

 This will both hit the instance where webLead should start as null and throw a DMLException (validation rules) ... I think.

sf.dev.ax1103sf.dev.ax1103

Thanks joshbirk for you reply. I am getting a save error Constructor not defined

 

 

 

 

@isTest private class testLeadClass{


static testmethod void saveLeadTest(){
       
    
         Lead l = new Lead(company='test company',lastname='xyz');
         test.starttest();

 


         Web2LeadExtension ext = new Web2LeadExtension(new ApexPages.StandardController(l));

 

PageReference p = ext.saveLead();
         test.stoptest();

 

system.assertEquals(Page.mypage.getUrl(),p.getUrl());
       
      }

   static testmethod void saveLeadTest2(){
        
        
        // Lead l = new Lead(company='testcompany');
         test.starttest();
         Web2LeadExtension ext = new Web2LeadExtension(new ApexPages.StandardController());
         PageReference p = ext.saveLead();
         test.stoptest();
         system.assertEquals(null,p);
        
     }


}

sf.dev.ax1103sf.dev.ax1103

static testmethod void saveLeadTest2(){
        
        
         Lead l = new Lead(company='testcompany');
         test.starttest();
         Web2LeadExtension ext = new Web2LeadExtension(new ApexPages.StandardController(l));
         PageReference p = ext.saveLead();
         test.stoptest();
         system.assertEquals(null,p);
        
     }

 

 

I am getting test class failure when i changed to the above code. "Required field missing Last Name"

joshbirkjoshbirk

My bad, needs to be at least:

 

Web2LeadExtension ext = new Web2LeadExtension(new ApexPages.StandardController(new Lead()));

 

Although as you have it modified should be fine.  I think the use case you've got in your extension is a bit broad, checking for wlead shouldn't be necessary as you can't get a null from the StandardController's getRecord.  It's either the record defined by the Id or a new Lead() by nature.

 

Also, DMLException may be to specific in the catch.  Try the extension like this:

 

public class Web2LeadExtension {

    public Lead wlead {get; set;}
  
    public Web2LeadExtension(ApexPages.StandardController
                                stdController) {
       wlead = (Lead)stdController.getRecord();
     }

     public PageReference saveLead() {
       try {
           insert(wlead);
       }
       catch(System.Exception e) {
           ApexPages.addMessages(e);
           return null;
       }
       PageReference p = Page.mypage;
       return p;
     }
}

 

See if that helps.

This was selected as the best answer
sf.dev.ax1103sf.dev.ax1103

Thanks joshbirk.