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
affuaffu 

Urgent: Please help to write Test class

public class SearchController
{
 public String nameQuery {get; set;}
 public List<Client__c> clients {get; set;}
  
 public PageReference executeSearch()
 {
  String queryStr='%' + nameQuery + '%';
  clients=[select id, Name
            from Client__c
            where name like :queryStr];
            
  return null;
 }
  
 public SearchController()
 {
  // if query appears in URL, execute it
  String urlQuery=ApexPages.currentPage().getParameters().get('query');
   
  if ( (null!=urlQuery) && (0!=urlQuery.length()) )
  {
   nameQuery=urlQuery;
   executeSearch();
  }
 }
}

 Please help me to write test class.....

 

Thanks in advance......

Best Answer chosen by Admin (Salesforce Developers) 
Amn12345Amn12345

@isTest(SeeAllData = true)
public static TestMethod  void TestController()
{
SearchController sc= new SearchController();

test.startTest();


PageReference ref = sc.executeSearch();

test.stopTest();
}

 

Hope this help!!

All Answers

Amn12345Amn12345

@isTest(SeeAllData = true)
public static TestMethod  void TestController()
{
SearchController sc= new SearchController();

test.startTest();


PageReference ref = sc.executeSearch();

test.stopTest();
}

 

Hope this help!!

This was selected as the best answer
netspidernetspider

Dont use (SeeAllData = true) - much better to create your own test data so you Client__c rec

PageReference ref = new PageReference('/apex/pagename?query=' + name of test data rec);

Test.setCurrentPage(ref);

 

Now you can do the rest as stated below

 

@isTest(SeeAllData = true)
public static TestMethod  void TestController()
{
SearchController sc= new SearchController();

test.startTest();


PageReference ref = sc.executeSearch();

test.stopTest();
}

 

affuaffu

Thank u very much it works fine....

netspidernetspider

Glad to help 

I would appreciate if you would mark my answer as Best answer it helps me, thanks

joojoo

Hi netspider,

 

First of all thanks for the given code. It is helpful for me. I have one query here. Can we validate pageReference by using system.assert() or system.assertEquals() which we are getting after executing executeSearch method ?

 

Please share you thoughts.

 

Thanks again!

 

Regards

Joo

 

 

netspidernetspider
Yes all you need to do is:

PageReference XPage = Page.XPage;

//Set ID
XPage.getParameters().put('ID', test.ID);

//Set Current Page
Test.setCurrentPage(XPage);

//Create Quote Add Subscriber Controller
XClass Xinstance = new XClass();

Truphone Limited, registered in England and Wales (registered company number: 04187081). Registered office: 4 Royal Mint Court, London EC3N 4HJ. VAT No. GB 851 5278 19

This e-mail, and any attachment(s), may contain information which is confidential and/or privileged, and is intended for the addressee only. If you are not the intended recipient, you may not use, disclose, copy or distribute this information in any manner whatsoever. If you have received this e-mail in error, please contact the sender immediately and delete it.