[SalesForce] Creating a tesclass error:Method does not exist or incorrect signature: void size() from the type accSearchController

Im strying to write a testclass but im stuc on the following error:Method does not exist or incorrect signature: void size() from the type accSearchController. See my class and testclass

public with sharing class accSearchController { 
public list <contact> cont {get;set;} 
public string searchstring {get;set;} 
public accsearchcontroller() { 
} 
public void search(){    

string searchquery='SELECT account.name,name from contact where Name like \'%'+searchstring+'%\' Limit 20'; 
cont= Database.query(searchquery); 
} 
public void clear(){ 
cont.clear(); 
} 
}

@isTest
public class accSearchControllerTestClass {

static testMethod void testaccSearchController() { 

     //create an account 
    Account acct = new Account(Name='testAccount');  
    insert acct;

    Contact c = new Contact(firstname='Test+k',lastname='Test+k',AccountId=acct.Id);
    insert c;

     //create an instance of controller.
    accsearchcontroller myController = new accsearchcontroller();
    system.debug(myController.size());
    System.assertEquals(1, myController.size());

    }
}

Best Answer

It seems you wish to asset agains the size of your cont property.

system.assertEquals(1, myController.cont.size(), 'Add an assertion message here');

It would be wise to first assert the property is not null. Otherwise, your test might at some point fail with a null pointer exception, rather than an assertion message. The latter tends to be much more informative.

system.assertNotEquals(null, myController.cont, 'The property should contain query results');
system.assertEquals(1, myController.cont.size(), 'Message...');

At this point you may catch that you haven't yet called myController.search(), which appears to be what you are trying to test.

Related Topic