You need to sign in to do that
Don't have an account?
Abhishek 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
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
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:
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!!!!
@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();
}
}
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
Also, you are not assigning your results to anything in the test.
Before
You'll need to do something like
to get the results out the controller.