+ Start a Discussion
RakeebRakeeb 

How can i do Testcode code coverage and what are the steps we have to follow?

HI SFdeveloper,Can anyone explain about test code coverage for 75% required.what is the common steps to follow test code coverage for every apex class .Here i have code with 0% Test code coverage so how can i do 75% .So kindly let me know ASAP.

APEX class:

public class theController {

 

   String searchText;


   List<Lead> results;

 

   public String getSearchText()

{
      return searchText;


   }

   public void setSearchText(String s) {


      searchText = s;


   System.debug('SEARCHTEXT:'+searcHText);
   }

   public List<Lead> getResults() {
      
     return results;
   }

   public PageReference doSearch() {
    

    results = (List<Lead>)[FIND :searchText RETURNING Lead(Name, Email, Phone)][0];
           
    return null;
   }
}

 

Visualforce page:

<apex:page controller="theController" showHeader="true">
<apex:messages />
   <apex:form >
      <apex:pageBlock mode="edit" id="block">
         <apex:pageBlockSection >
            <apex:pageBlockSectionItem >
               <apex:outputLabel for="searchText">Search Text</apex:outputLabel>
               <apex:panelGroup >
                  <apex:inputText id="searchText" value="{!searchText}"/>
                  <apex:commandButton value="Go!" action="{!doSearch}"
                                      rerender="block" status="status"/>
               </apex:panelGroup>
            </apex:pageBlockSectionItem>
        </apex:pageBlockSection>
        <apex:actionStatus id="status" startText="requesting..."/>
        <apex:pageBlockSection title="Results" id="results" columns="1">
           <apex:pageBlockTable value="{!results}" var="l"
                               rendered="{!NOT(ISNULL(results))}">
              <apex:column value="{!l.name}"/>
              <apex:column value="{!l.email}"/>
              <apex:column value="{!l.phone}"/>
           </apex:pageBlockTable>
        </apex:pageBlockSection>
      </apex:pageBlock>
   </apex:form>
</apex:page>

Best Answer chosen by Admin (Salesforce Developers) 
kiranmutturukiranmutturu

this will give you the basic information....

http://wiki.developerforce.com/index.php/An_Introduction_to_Apex_Code_Test_Methods

 

 

and for ur code the basic code coverage code will be like this.. but you can do it better..

@isTest

private class testmyclass{   

static testmethod void mytestname(){   

    theController  obj = new theController();       

obj.getSearchText(); 

      obj.setSearchText('test');       

obj.getResults();     

  obj.doSearch(); 

  }

}

 

All Answers

kiranmutturukiranmutturu

this will give you the basic information....

http://wiki.developerforce.com/index.php/An_Introduction_to_Apex_Code_Test_Methods

 

 

and for ur code the basic code coverage code will be like this.. but you can do it better..

@isTest

private class testmyclass{   

static testmethod void mytestname(){   

    theController  obj = new theController();       

obj.getSearchText(); 

      obj.setSearchText('test');       

obj.getResults();     

  obj.doSearch(); 

  }

}

 

This was selected as the best answer
RakeebRakeeb

Kiran Thanks a lot  for instantly giving the solution.Can have your email id so that if i have any further queries i will post to  you.So kindly provide your email id it will be helpfull to me.

 

 

Muruganand_SforceMuruganand_Sforce

Hi Kiran,

 

Are you Sure that  "obj.getSearchText(); " -----This line wil work out as you try to get input in Test class...

 

Muruganand_Sforce