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
donsu04donsu04 

Test Code Coverage

Hello there.

 

I am looking for some help getting code coverage to visual force page.  My first page :) , So any help will be much appreciated.

 

Thanks 

Sudhir

 

 

 

Apex Class :

 

public class SubmitLeadController {
    
    public Lead L { get; set; }
    public SubmitLeadController() {
        L = new Lead();
        L.Company = 'Not Available';
        L.IsRAALead__c = true;
    }
    
      public Attachment A1 {
  get {
      if (A1 == null)
        A1 = new Attachment();
      return A1;
    }
  set;
  }
    public Attachment A2 {
  get {
      if (A2 == null)
        A2 = new Attachment();
      return A2;
    }
  set;
  }
       
    
    public PageReference submitLead() {
     
            try {

                INSERT L;
                
            IF (A1.Body!=null)
            {
            
                  A1.OwnerId = UserInfo.getUserId();
                  A1.IsPrivate = true;
                  A1.ParentId = L.id;
                  insert A1;
            }
            
            IF   (A2.Body!=null)
            {    
                   A2.OwnerId = UserInfo.getUserId();
                  A2.IsPrivate = true;
                  A2.ParentId = L.id;
                  insert A2;
             }     
                  
                return new PageReference('/RAASubConf');
            } catch (Exception e) {
                ApexPages.addMessages(e);
                return null;
            }
                       
    }
  
  
    
        }

 

Visual Force page :

 



<apex:page standardstylesheets="false" showheader="false" sidebar="false" controller="SubmitLeadController"> <font face="verdana"> <h3>Web To Lead With Attachments </h3> <apex:form > <apex:pageMessages /> <table> <tr></tr> <tr> <th>First Name:</th> <td><apex:inputText required="true" value="{!L.Firstname}"/></td> </tr> <tr> <th>Last Name:</th> <td><apex:inputText required="true" value="{!L.Lastname}"/></td> </tr> <tr> <th>Email:</th> <td><apex:inputText required="true" value="{!L.Email}"/></td> </tr> <tr> <th>Phone: </th> <td><apex:inputText required="true" value="{!L.Phone}"/></td> </tr> <tr> <th>Lead Source Notes:</th> <td><apex:inputTextArea rows="6" value="{!L.Lead_Source_Details__c}"/></td> </tr> <tr> <th>Attachment 1:</th> <td><apex:inputFile value="{!A1.body}" filename="{!A1.name}"/></td> </tr> <tr> <th>Attachment 2:</th> <td><apex:inputFile value="{!A2.body}" filename="{!A2.name}"/></td> </tr> <tr> <td><apex:commandButton value="Submit Lead" action="{!submitLead}"/></td> </tr> </table> </apex:form> </font> </apex:page>


Jeff MayJeff May

Here is a sample testmethod to demonstrate the approach. Your testmethod would of course be different.

 

    @IsTest (SeeAllData=true)
    static void testMyPage(){
        
        Opportunity opp = [select Id from Opportunity limit 1];
               
        // load the page       
        PageReference pageRef = Page.MyPage;
        pageRef.getParameters().put('Id',opp.Id);
        Test.setCurrentPageReference(pageRef);
        
        // load the extension
        myPageExtension mpe = new myPageExtension(new ApexPages.StandardController(opp));
        
        //test controller methods
        mpe.searchString = 'Joe Test';
        mpe.updateContacts();
        system.assert(mpe.contactsFound()==1);
}
     

 

venkateshyadav1243venkateshyadav1243

try these it cover some part and check these may be it will help you

 

 

@istest
public class leadsubmit{
public static testMethod void SubmitLeadController()
{
Lead l=new Lead();
l.Company='test';
l.LastName='venki';
l.Status='open';
l.IsRAALead__c = true;
insert l;
Attachment attach=new Attachment();
attach.Name='Unit Test Attachment';
Blob bodyBlob=Blob.valueOf('Unit Test Attachment Body');
attach.body=bodyBlob;
attach.parentId=l.id;
insert attach;
SubmitLeadController slc=new SubmitLeadController();
slc.submitLead();
}
}