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
Guru 91Guru 91 

How to write test class for Search class?

Hello Every One,
I have a class for Searching a record in quote and i written test class for that and it is fail. 
Can you please help me with that 
Apex Class:

public with sharing class accsearchcontroller 
{

    public accsearchcontroller(ApexPages.StandardController controller) {

    }


    public String l { get; set; }
public list <quote> acc {get;set;}
public string searchstring {get;set;}
public String quoteid{get; set;}
public Boolean search{get; set;}
public Boolean result{get; set;}
public accsearchcontroller( ) 
{
}
public void search(){
string searchquery='select QuoteNumber,Name,Email,Opportunity.Name,CBG_SAPQuoteNumber__c  from quote where CBG_SAPQuoteNumber__c like \'%'+searchstring+'%\' OR CBG_SAPQuoteNumber__c like \'*'+searchstring+'*\'  Limit 20';
acc= Database.query(searchquery);
}
public void clear(){
acc.clear();

}
public PageReference processLinkClick() {
         
        search = false;
        result = false;
        //Pagereference p1 = new PageReference('https://ap7.salesforce.com/'+quoteid);
        Pagereference p1 = new PageReference('https://cs84.salesforce.com/'+quoteid);
        p1.setRedirect(true);
        return p1;
    }
}
sfdcMonkey.comsfdcMonkey.com
Hi Guru ,
We will recommend you to start using trailhead to learn about test classes
1) https://trailhead.salesforce.com/modules/apex_testing

Also please check below post
1)http://https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_qs_test.htm
2)http://https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing_example.htm

sample Test classes
1) http://amitsalesforce.blogspot.com/2015/06/best-practice-for-test-classes-sample.html
@isTest
public class accsearchcontrollerTest 
{
	static testMethod void myUniteTest() {
	
		// Create your test data here for quote object
		
		Test.startTest();
			accsearchcontroller Obj = new accsearchcontroller();
			Obj.processLinkClick();
			Obj.clear();
			Obj.search();
		Test.stopTest();
		
		//Add Assert here	
	}
}

i hope it helps you.  Let me inform if it helps you and kindly mark it best answer if it helps you so it make proper solution for others
    thanks 
   
Guru 91Guru 91
Thank You piyush,
 Here is not my updated code but still not pass

@isTest
Public class accsearchcontrollertest
{
static TestMethod void testaccsearchcontroller()
{
  Quote qt = new Quote();
  qt.Name = 'Guru';
  qt.CBG_Employee_Responsible__c = 'Manish';
  qt.CBG_Incoterms__c = 'CFR';
  qt.CBG_PaymentTerms__c = 'CO04 payable immediatly';
  qt.Format__c = 'List';
  insert qt;
  Test.startTest();
  accsearchcontroller Obj = new accsearchcontroller();
  Obj.processLinkClick();
  Obj.clear();
  Obj.search();
  Test.stopTest();
  
}

}
sfdcMonkey.comsfdcMonkey.com
what error you have getting ? can you please share 
v varaprasadv varaprasad
Hi Guru,

Please call below line in test class : 
obj.searchstring = Guru'';

Obj.processLinkClick();
  Obj.clear();
  Obj.search();
  Test.stopTest();

Hope it helps you.

Thanks
Varaprasad
Guru 91Guru 91
Hello piyush,
No error in the class, but test class fail to cross 75% code coverage