+ Start a Discussion
NikiG22NikiG22 

Help on a Controller Test

I have a page redirect/Save controller and need help wrighting a test class for it.  I have 19% covered right now and need 100%

 

Any help would be appriciated

Niki

 

Controller and Test:

public class mywebLeadController {
    Lead lead;

    public Lead getLead() {
        if(lead == null) lead = new Lead();
        return lead;
    }

    public PageReference savelead() {
        // Add the lead to the database. 
        insert lead;
        
        // Send the user to the landing page for the new lead.
        PageReference leadPage = page.WebLeadLandingPage;
        leadPage.setRedirect(true);
        return leadPage;
    }
    
   
   
    @IsTest static void testSave() { 
            
    Lead lead = new Lead ();
    mywebLeadController pjae = new mywebLeadController (); 
            
    Lead.FirstName = 'Test';
    Lead.LastName = 'Test';
    Lead.Company = 'OTJ - NG';
    Lead.Email = 'test@otj.com';
    
    insert lead;
    
    PageReference leadPage = page.WebLeadLandingPage;
    leadPage.setRedirect(true);
    
    System.debug('the current page is...' +ApexPages.currentPage() );
       
    
    }
}//End

 

T.AizawaT.Aizawa

use "getLead" and "savelead" method

but, "lead" data isn't editable in mywebLeadController...

 

public class mywebLeadController {
    Lead lead;

    public void setLead(String firstname,String LastName,String company,String email){
lead = new Lead(); lead.FirstName = firstname; lead.LastName = lastname; lead.Company = company; lead.Email = email; }
... }

 

static testmethod void testSave(){
    mywebLeadController pjae = new mywebLeadController ();
/* getLead() */ Lead lead = pjae.getLead();
/* edit lead */ pjae.setLead('test','test','OTJ - NG','test@otj.com');

/* savelead() and check redirect */
PageReference leadPage = page.WebLeadLandingPage;
System.assertEquals(leadPage, pjae.savelead()); }

 

 

NikiG22NikiG22

Awesome! thank you so much for your help

 

Final Code:

public class mywebLeadController {
    Lead lead;

    public Lead getLead() {
        if(lead == null) lead = new Lead();
        return lead;
    }

    public void setLead(String firstname,String LastName,String company,String email){
       lead = new Lead();
       lead.FirstName = firstname;
       lead.LastName = lastname;
       lead.Company = company;
       lead.Email = email;
  }
    public PageReference savelead() {
        // Add the lead to the database. 
        insert lead;
        
        // Send the user to the landing page for the new lead.
        PageReference leadPage = page.WebLeadLandingPage;
        leadPage.setRedirect(true);
        return leadPage;
    }
    
   
   
static testmethod void testSave(){
    mywebLeadController pjae = new mywebLeadController ();
    /* getLead() */
    Lead lead = pjae.getLead();
    /* edit lead */
    pjae.setLead('test','test','OTJ - NG','test@otj.com');

    /* savelead() and check redirect */
    PageReference leadPage = page.WebLeadLandingPage;
    System.assertEquals(leadPage, pjae.savelead());
    
}
}//End

 

NikiG22NikiG22

when i try to deploy the final code i get the following error?

Failure Message: "System.AssertException: Assertion Failed: Expected: System.PageReference[/apex/webleadlandingpage], Actual: System.PageReference[/apex/webleadlandingpage]", Failure Stack Trace: "Class.mywebLeadController.testSave: line 37, column 1"

  Any ideas?

T.AizawaT.Aizawa

oh... sorry, i mistake

 

line 37:

 System.assertEquals(leadPage.getURL(), pjae.savelead().getURL());