You need to sign in to do that
Don't have an account?

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......
@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
@isTest(SeeAllData = true)
public static TestMethod void TestController()
{
SearchController sc= new SearchController();
test.startTest();
PageReference ref = sc.executeSearch();
test.stopTest();
}
Hope this help!!
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();
}
Thank u very much it works fine....
Glad to help
I would appreciate if you would mark my answer as Best answer it helps me, thanks
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
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.