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
cjencjen 

Test method for get set

Hi, I am trying to write a test method fo rthe following. I am a total newbie when it comes to code, so need lots of help, please.

public with sharing class accsearchcontroller { 
   public list <account> acc {get;set;} 
   public string searchstring {get;set;} 
   public accsearchcontroller(ApexPages.StandardController controller) { 
   } 
   public void search(){ 
     string searchquery='select OwnerName__c,name,owner.phone,Account_Manager__c,BDM__c,BillingState,BillingCity, id from account where (name like \'%'+searchstring+'%\' OR OwnerName__c like \'%'+searchstring+'%\') Limit 1000'; 
     acc= Database.query(searchquery); 
   } 
   public void clear(){ 
   acc.clear(); 
   } 
 }


Here is what i wrote, but gets an error, Error: Compile Error: Constructor not defined: [accsearchcontroller].<Constructor>() :

@isTest
public class accsearchcontrollerTest{
static testMethod void test() {
    accsearchcontroller acc = new accsearchcontroller();
    Test.startTest();
    account[] accounts = acc.getAccounts();
    Test.stopTest();
    System.assertNotEquals(null, accounts);
}
}


Please advise, thanks!!!!!
Roy LuoRoy Luo
See here for how to unit test VF and its controller:

http://salesforce.stackexchange.com/questions/40901/visualforce-controller-unit-test-vf-page-displays-fullcalendar

In your test, instantiate your controller, then call ctrl.search(); ctrl.clear();
Anoop yadavAnoop yadav
Hi,
Try the below code.
@isTest(seeAllData = false)
public class AccSearchControllerTest {

	public static testMethod void testMethod() {
	
		List<Account> accList = new List<Account>();
		Account acc = new Account();
		acc.Name = 'Test Account';
		// Put all mandatory fields 
		insert acc;
		
		accList.add(acc);
		
		ApexPages.StandardController sc = new ApexPages.StandardController(acc);
		accsearchcontroller accSearch = new accsearchcontroller(sc);
		
		Test.startTest();
			accSearch.searchstring = 'Test Account';
			accSearch.search();
			accSearch.clear();
			
		Test.stopTest();
	}
}

 
AshlekhAshlekh
Hi ,

Use below code
static testmethod void validateStandardController(){
         
	Test.StartTest(); 
        Account testAccount= new Account(name = 'Account Plan Test123');
	insert testAccount;
        
        ApexPages.StandardController sc = new ApexPages.StandardController(testAccount);
        accsearchcontroller  testAccPlan = new accsearchcontroller (sc);
         account[] accounts = acc.getAccounts();
        Test.stoptest();
        
      }