0

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());

    }
}
1
  • Your main class has search() method where as you are trying to call size() inside test code. size() is one of the List method that you can use in apex code.
    – Devendra
    Commented Aug 30, 2017 at 21:56

1 Answer 1

3

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.

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .