You need to sign in to do that
Don't have an account?
K@S
test class for didn't get the save.
Hi Team,
This my apex controller and I wrote test class for this related but not working(test class).
Could you please help me on this.
public class searchClientVisits{
List<Client_Visit__c> clientVisitList {get;set;}
public Client_Visit__c clientInfo { get;set;}
public SearchClientVisits(){
clientInfo = new Client_Visit__c ();
}
//get a client visit list
public List<Client_Visit__c> getclientVisitList(){
return clientVisitList;
}
//search the client records
public void runSearch(){
if(clientInfo.Start_Date__c != null && clientInfo.End_Date__c != null){
Date startDate = clientInfo.Start_Date__c;
System.debug('#####'+startDate);
Date endDate = clientInfo.End_Date__c;
System.debug('#####'+endDate);
clientvisitList = [SELECT Id,Accounts__c,Start_Date__c,End_Date__c,Clients__c,Locations__c FROM Client_Visit__c WHERE Start_Date__c >= :startDate AND End_Date__c <=:endDate];
System.debug('#####'+clientvisitList);
}
}
public PageReference reset(){
PageReference newpage = new PageReference(System.currentPageReference().getURL());
newpage.setRedirect(true);
return newpage;
}
======================================================
Test class
@isTest
Public class clientVistTest{
static testMethod void tsetSearchList() {
clientvisitList cv = new clientvisitList();
clientvisitList = [SELECT Id,Accounts__c,Start_Date__c,End_Date__c,Clients__c,Locations__c FROM Client_Visit__c WHERE Start_Date__c >= :startDate AND End_Date__c <=:endDate];
System.assert(!cv = null );
Test.startTest();
PageReference pageRef = Page.reset;
Test.setCurrentPageReference(pageRef);
Test.stopTest();
}
}
This my apex controller and I wrote test class for this related but not working(test class).
Could you please help me on this.
public class searchClientVisits{
List<Client_Visit__c> clientVisitList {get;set;}
public Client_Visit__c clientInfo { get;set;}
public SearchClientVisits(){
clientInfo = new Client_Visit__c ();
}
//get a client visit list
public List<Client_Visit__c> getclientVisitList(){
return clientVisitList;
}
//search the client records
public void runSearch(){
if(clientInfo.Start_Date__c != null && clientInfo.End_Date__c != null){
Date startDate = clientInfo.Start_Date__c;
System.debug('#####'+startDate);
Date endDate = clientInfo.End_Date__c;
System.debug('#####'+endDate);
clientvisitList = [SELECT Id,Accounts__c,Start_Date__c,End_Date__c,Clients__c,Locations__c FROM Client_Visit__c WHERE Start_Date__c >= :startDate AND End_Date__c <=:endDate];
System.debug('#####'+clientvisitList);
}
}
public PageReference reset(){
PageReference newpage = new PageReference(System.currentPageReference().getURL());
newpage.setRedirect(true);
return newpage;
}
======================================================
Test class
@isTest
Public class clientVistTest{
static testMethod void tsetSearchList() {
clientvisitList cv = new clientvisitList();
clientvisitList = [SELECT Id,Accounts__c,Start_Date__c,End_Date__c,Clients__c,Locations__c FROM Client_Visit__c WHERE Start_Date__c >= :startDate AND End_Date__c <=:endDate];
System.assert(!cv = null );
Test.startTest();
PageReference pageRef = Page.reset;
Test.setCurrentPageReference(pageRef);
Test.stopTest();
}
}
Try below code ,replace your Vf page in code ,Add any mandatory fields in Account and contact level .
Let me knwo if it helps !!
All Answers
In the test class you need to create the instance of the main class like below
Still I got the error.how to write testclass for related apex controller.
could please let me know.
Error: Compile Error: Invalid type: clientvisitList at line 4 column 34
@isTest
Public class clientVistTest{
static testMethod void tsetSearchList() {
clientvisitList cv = new clientvisitList();
//Controller
searchClientVisits controller = new searchClientVisits();
//Define methods
controller.runSearch();
clientvisitList = [SELECT Id,Accounts__c,Start_Date__c,End_Date__c,Clients__c,Locations__c FROM Client_Visit__c WHERE Start_Date__c >= :startDate AND End_Date__c <=:endDate];
System.assert(!clientvisitList = null );
Test.startTest();
PageReference pageRef = Page.reset;
Test.setCurrentPageReference(pageRef);
Test.stopTest();
}
}
List<Client_Visit__c> clientvisitList = new List<Client_Visit__c>();
You have not created the instance of class that is why it is not saving . Create the instance of class as given below.
Try this below code ,it will work fine
@isTest
private class TestSearchClientVisit
{
static testMethod void insertClientVisit()
{
// Here mention all your required field values.
Client_Visit__c cv = new Client_Visit__c();
cv.Name = 'Test client visit';
insert cv;
SearchClientVisits scv = new SearchClientVisits();
List<Client_Visit__c> cvivar= scv.clientVisitList;
Client_Visit__c cvar=scv.clientInfo;
List<Client_Visit__c> cvist=scv.getclientVisitList();
scv.runSearch();
scv.reset();
}
}
If you get the answer, please mark it as the correct answer. It will be a help to others who are facing the same problem later.
Regards
Eswar Prasad
Try with below code and try to replace your VF page Name in code !
Let us know if it helps
Try below code ,replace your Vf page in code ,Add any mandatory fields in Account and contact level .
Let me knwo if it helps !!