+ Start a Discussion
Abhishek Yadav 73Abhishek Yadav 73 

Hi Guys, I am trying to write a test class code for my VF page which searches the database for records using any field.

//The custom controller is as given below

public class practice {
    
    public String SearchText {get; set;}
    
    public List<Task_Tracker__c> TrackList {get; set;}
    public PageReference doSearch(){
        List<List<sobject>> records = new List<List<sobject>>();
        List<Task_Tracker__c> NewList= new List<Task_Tracker__c>();
        
        //SEARCH FOR STARTDATE
        IF(SearchText.contains('/'))
        {
            Date dat = Date.parse(SearchText);
            NewList = [select Name,Project__c,Description__c,Priority__c,
                       ClosedDate__c,StartDate__c from Task_Tracker__c where StartDate__c = :dat];
            NewList = [select Name,Project__c,Description__c,Priority__c,
                       ClosedDate__c,StartDate__c from Task_Tracker__c where ClosedDate__c = :dat];
            TrackList = NewList;
            
        }
        
        
        
        //SEARCH FOR STATUS  
        else If(SearchText=='In Progress'|| SearchText=='New'||  SearchText=='On Hold' ||  SearchText=='Not Started'||
                SearchText=='Reassigned' || SearchText=='Rejected'|| SearchText=='Closed' || SearchText=='Escalated')
        {
            NewList = [select Name,Project__c,Description__c,Priority__c,
                       ClosedDate__c,StartDate__c from Task_Tracker__c where Status__c = :SearchText];
            TrackList = NewList;
            
        }
        
        //SEARCH FOR PRIORITY
        else If(SearchText=='High'|| SearchText=='Medium'||  SearchText=='Low' )
        {
            NewList = [select Name,Project__c,Description__c,Priority__c,
                       ClosedDate__c,StartDate__c from Task_Tracker__c where Priority__c = :SearchText];
            TrackList = NewList;
            
        }
        else
        {
            
            //SERACH FOR RESOURCEEMAIL OR NAME
            
            records = [FIND :SearchText RETURNING Task_Tracker__c(Name,Project__c,Description__c,Priority__c,
                                                                  ClosedDate__c,StartDate__c)] ;
            TrackList=records[0];
            
        }
        //List of sobject, so records will be of tt
        return null;
    }      
}


//AND THE VF PAGE GOES LIKE THIS

<apex:page controller="practice">
    <apex:form >
              <apex:pageBlock >
              <apex:pageBlockSection title="Search Candidate">
              <apex:inputText label="Enter Search Text" value="{!SearchText}"/>
              <!--         <apex:inputText label="Enter Search Text" value="{!SearchDate}"/>-->
               <apex:commandButton value="Do Search" action="{!doSearch}" reRender="cd"/>
                <!--reReder to identify what to refresh-->
            </apex:pageBlockSection>
            <!--id to identify what to refresh --implementation of ajax-->
            <apex:pageBlockSection title="Search Details" id="cd">
                <apex:pageBlockTable value="{!TrackList}" var="can">
                             <apex:column value="{!can.Name}"/>
                    <apex:column value="{!can.Project__c}"/>
            <!--        <apex:column value="{!can.Status__c}"/>-->
                   <apex:column value="{!can.Description__c}"/>
                </apex:pageBlockTable>
            </apex:pageBlockSection>
              </apex:pageBlock>
    </apex:form>
</apex:page>

//Can anybody hel;p me out with the test class please
srlawr uksrlawr uk
Hi Abhishek,

The best place to kick off learning about test classes is with Trailhead, this module introduces Tests, the structure and annotations used for them:

https://trailhead.salesforce.com/en/modules/apex_testing/units/apex_testing_intro


You'll probably end up with something looking like:
 
@isTest
private class practiceTest {

    @isTest
    static void testDoSearch_startDate() {
        // test code specific to a Search text with a '/' in...
    }

    @isTest
    static void testDoSearch_startDate() {
        // test code specific to a Search text "In Progress"
    }

    @isTest
    static void testDoSearch_startDate() {
        // test code specific to a Search text of "high"
    }

}

You will have a little fun inserting the test data here, I wouldn't recommend using @testSetup - as each of your scenarios will require a different set of test data, so just insert it in each method!

I hope that helps, and gets you on the road to quality testing!

Don't forget your System.asserts!!!!
Abhishek Yadav 73Abhishek Yadav 73
THIS IS HOW I TRIED TO WRITE BUT ITS NOT EVEN SHOWING THE CODE COVERAGE


@isTest
public class TaskSearchTest {
    @isTest
     static void testing() {
        List<Task_Tracker__c> TrackList = new List<Task_Tracker__c>();
         Task_Tracker__c t1 = new Task_Tracker__c(StartDate__c=Date.parse('12/2/2017'),ClosedDate__c=Date.parse('12/15/2017'),
                                                     Name='ABCDE',Escalated__c= true, Description__c='trtrtrt',Project__c='a0M0l000001TyC8',Parent__c='a0O0l0000022wmb');
            TrackList.add(t1);
        
        insert TrackList;
       //Use the PageReference Apex class to instantiate a page
       PageReference pageRef = Page.TaskSearchPage;
        practice testsearch = new practice();
         pageRef.getParameters().put( 'ABC','Name');
         for(Task_Tracker__c listin : TrackList)
          System.assertEquals('ABCDE',listin.Name, 'MisMatch');

        // Test.setCurrentPage(pageRef);
   //   for(Task_Tracker__c listin : TrackList)
     //   testsearch.doSearch();
     
    }
        

    

}
srlawr uksrlawr uk
Hmm. What are you expecting the line
 
pageRef.getParameters().put( 'ABC','Name');

expected to do? I don't see in your code any reference to the page Params?

I think you want to set SearchText in your controller, with something like
 
practice testsearch = new practice();
testSearch.SearchText = 'High';

Also, you are not assigning your results to anything in the test.

Before 
 
for(Task_Tracker__c listin : TrackList)

You'll need to do something like
 
List<Task_Tracker__c> results = practice.TaskList;

to get the results out the controller.