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
Madevala JithenderMadevala Jithender 

How do i write test class for Contact Search Method

public class ContactSearch {
    public static list<contact> searchforcontacts(string lastname,string mailingpostalcode){
        list<contact> contacts = 
            [select id,name,lastname,mailingpostalcode from contact where lastname = :lastname and mailingpostalcode = :mailingpostalcode];
        system.debug('found contacts:' + contacts);
        return contacts;

Best Answer chosen by Madevala Jithender
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Madevala,

Can you try the below test class with positive and negative test cases. It covers 100% of your code.
public class ContactSearchTest {
static testMethod void myTest() {
    Account acc = new Account();'sample';
    insert acc;
    Contact con= new contact();
    insert con;
    List<Contact> conlist=ContactSearch.searchforcontacts('sample','1234');
    system.assertEquals(1, conlist.size());
    List<Contact> conlist1=ContactSearch.searchforcontacts('sample123','1234');
    system.assertEquals(0, conlist1.size());

Let me know if you face any issues.

If this solution helps, Please mark it as best answer.