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
ktshannonktshannon 

Problem with testClass for Sites To Lead Form Cookbook code.

Hey all,

 

I am having issues creating a testClass for a sample code from the Cookbook.

http://developer.force.com/cookbook/recipe/creating-a-web-to-lead-form-for-your-force-com-site

 

The controller is very simple and can be seen here:

public class myWeb2LeadExtension {

    private final Lead weblead;

    public myWeb2LeadExtension(ApexPages.StandardController
                                stdController) {
       weblead = (Lead)stdController.getRecord();
    }

     public PageReference saveLead() {
       try {
       insert(weblead);
       }
       catch(System.DMLException e) {
           ApexPages.addMessages(e);
           return null;
       }
       PageReference p = Page.thankyou_getinvolved;
       p.setRedirect(true);
       return p;
     }
}

 

I have written the testClass to insert all required fields for the Lead, but it is still not validating the class. What am I missing?

 

Thank you for your help!

 

joshbirkjoshbirk

Can you post your testClass / unit test?  

sf.dev.ax1103sf.dev.ax1103

@isTest private class testLeadClass{

//positive test
static testmethod void saveLeadTestOK(){
         
         // populate a lead
         Lead l = new Lead(company='testcompany',lastname='testln');
         test.starttest();

 

 // instantiate a controller extension
         myWeb2LeadExtension ext = new myWeb2LeadExtension(new ApexPages.StandardController(l));

// call the saveLead method 

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

// make sure that everything worked as expected and the user is redirected to the Thank You page 

system.assertEquals(Page.ThankYou.getUrl(),p.getUrl());
         
         
     }
     
//negative test
static testmethod void saveLeadTestKO(){
         
         
         Lead l = new Lead(company='testcompany');
         test.starttest();
         myWeb2LeadExtension ext = new myWeb2LeadExtension(new ApexPages.StandardController(l));
         PageReference p = ext.saveLead();
         test.stoptest();
         system.assertEquals(null,p);
         
     }
}



joshbirkjoshbirk

Any change in behavior if the assertEquals occurs before stopTest?

sf.dev.ax1103sf.dev.ax1103

No Change in behavior

joshbirkjoshbirk

When you say it isn't validating the class, do you mean there's specifc code coverage not getting hit?  Or do the assertEquals fail?

sf.dev.ax1103sf.dev.ax1103

Hi joshbirk,

 

          my  test class is  for ktshannon's class.Sorry for whole confusion.

 

Thanks

joshbirkjoshbirk

Ah ha!  That makes more sense...

sf.dev.ax1103sf.dev.ax1103

Hi joshbirk,

   

          I am having coverage issue.Can you look into my post.

 

 

        http://boards.developerforce.com/t5/Apex-Code-Development/test-class-coverage/td-p/338073

 

 

Thanks