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
K@SK@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();
    }
}
 
Best Answer chosen by K@S
ManojjenaManojjena
Hi kae,

Try below code ,replace your Vf page in code ,Add any mandatory fields in Account and contact level .
 
@isTest
private class TestSearchClientVisit{
	public static testMethod void unitTest(){
		Account acc=new Account();
			acc.Name='TestAccount';
			//Add mandatory fields for account below
			insert acc;
		Contact con=new Contact();
			con.LastName='Testontact';
			con.Email='kae@gmail.com';
			//Add mandatory fields for contact below
			insert con;
		Client_Visit__c clv = new Client_Visit__c();
			clv.Name = 'Test client visit';
			clv.Acoount__c=acc.id;
			clv.client__c=con.Id;
			clv.Start_Date__c = System.today();
			clv.End_Date__c = System.today().addDays(5);
			insert clv;
		SearchClientVisits scv = new SearchClientVisits();
			PageReference pageRef = Page.Your VF page Name; 
			Test.setCurrentPage(pageRef);
		   scv.clientInfo=clv;
		   scv.runSearch();
		   scv.reset();
		   scv.getclientVisitList();
	}
}

Let me knwo if it helps !!

All Answers

Abhi_TripathiAbhi_Tripathi
It is not saving as you have not created the instance of the class 
In the test class you need to create the instance of the main class like below
 
//Controller
searchClientVisits controller = new searchClientVisits();

//Define methods now to test them
controller.runSearch();

 
K@SK@S
Hi Abhi,

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();
    }
}
Krishna SambarajuKrishna Sambaraju
To fix the above error change the code on line 4 as below.

List<Client_Visit__c> clientvisitList = new List<Client_Visit__c>();
Ajay K DubediAjay K Dubedi
Hi,
You have not created the instance of class that is why it is not saving . Create the instance of class as given below.
searchClientVisits obj=new searchClientVisits();
obj.methodName();

 
Eswar Prasad@Sfdc11Eswar Prasad@Sfdc11
HI
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
ManojjenaManojjena
Hi Kae,

Try with below code and try to replace your VF page Name in code !
 
@isTest
private class TestSearchClientVisit{
	public static testMethod void unitTest(){
		Client_Visit__c clv = new Client_Visit__c();
			clv.Name = 'Test client visit';
			clv.Start_Date__c = System.today();
			clv.End_Date__c = System.today().addDays(5);
			insert clv;
		SearchClientVisits scv = new SearchClientVisits();
			PageReference pageRef = Page.Your VF page Name; 
			Test.setCurrentPage(pageRef);
		   scv.clientInfo=clv;
		   scv.runSearch();
		   scv.reset();
		   scv.getclientVisitList();
	}
}

Let us know if it helps 
ManojjenaManojjena
Hi kae,

Try below code ,replace your Vf page in code ,Add any mandatory fields in Account and contact level .
 
@isTest
private class TestSearchClientVisit{
	public static testMethod void unitTest(){
		Account acc=new Account();
			acc.Name='TestAccount';
			//Add mandatory fields for account below
			insert acc;
		Contact con=new Contact();
			con.LastName='Testontact';
			con.Email='kae@gmail.com';
			//Add mandatory fields for contact below
			insert con;
		Client_Visit__c clv = new Client_Visit__c();
			clv.Name = 'Test client visit';
			clv.Acoount__c=acc.id;
			clv.client__c=con.Id;
			clv.Start_Date__c = System.today();
			clv.End_Date__c = System.today().addDays(5);
			insert clv;
		SearchClientVisits scv = new SearchClientVisits();
			PageReference pageRef = Page.Your VF page Name; 
			Test.setCurrentPage(pageRef);
		   scv.clientInfo=clv;
		   scv.runSearch();
		   scv.reset();
		   scv.getclientVisitList();
	}
}

Let me knwo if it helps !!
This was selected as the best answer