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
Sherwin BetontaSherwin Betonta 

Test Class for Search and delete contact method

I'm new to Apex. I would appreciete if somebody can help me with these codes. I can't seem to cover them on my test class. both else statement were the only one's not covered. 

Delete Contact
public static void processBeforeDeleteTrigger(List<Contact> oldContact, Map<Id, Contact> oldContactMap){
        Id accountRelatedID;
        for(Contact con : oldContact){
            accountRelatedID = con.AccountID;
        }
        Account relatedAccount = [SELECT Id, (SELECT ID FROM Contacts) FROM Account WHERE Id =:accountRelatedID];
        List<Contact> conList = new List<Contact>(relatedAccount.Contacts);
        system.debug('number of contacts' + conList.size());
        if(conList.size()==1){
             //this part is not covered
            oldContact[0].addError('Remaining contact cannot be deleted!');
        }

    }

Test Class
public static void deleteContactTEST() {
        Account newAccount = new Account(Name='Test Account Name');
        insert newAccount;
         List<Contact> newContact = new List<Contact>();
         for (Integer count = 0; count < 50; count++) {
             newContact.add(new Contact (LastName=' Test Contact Name' + count, AccountId = newAccount.Id));
         }
        insert newContact;
        Test.startTest();
         Boolean status = RelatedContactController.deleteContact(newContact[0].id);
        Test.stopTest();
         
         List<Contact> getContact = [SELECT Id FROM Contact WHERE AccountId = :newAccount.Id];
         System.assertEquals ( 49 , getContact.size());
         System.assertEquals ( true , status);
         
     }
Search Account
public static string searchQuery(String searchKey, Integer recordToDisply) {
        String name = '%' + searchKey + '%';
        //String finalString = name.replaceAll('\\p{Punct}', '');
        String soqlInject = String.escapeSingleQuotes(name);
        //String SOQL = 'SELECT Id, Name,(select Id from Contacts) FROM Account WHERE Name LIKE :\'' + soqlInject + '\'' + 'ORDER BY Name LIMIT :recordToDisply';
        List<Object> accountList= new List<Object>();
        //Implement a Search Query with SOQL when conditions are met
        if(searchKey!='' || searchKey != null){
            accountList = [SELECT Id, Name,(select Id from Contacts) FROM Account WHERE Name LIKE : soqlInject ORDER BY Name LIMIT :recordToDisply];
        }
        else{
            //this is not covered
            accountList = [SELECT Id, Name,(select Id from Contacts) FROM Account ORDER BY Name LIMIT :recordToDisply];
        }
        //convert data types into JSON format
        return JSON.serialize(accountList);
    }

Test class:
public static void findByNameTest(){
        Integer count = [select count() from account];
        test.startTest();
        List<Object> test4 = (List<Object>)JSON.deserializeUntyped(AccountContactController.searchQuery('Test',6));
        List<Object> test5 = (List<Object>)JSON.deserializeUntyped(AccountContactController.searchQuery('',30));
        test.stopTest();
        system.assertEquals(6, test4.size());
        system.assertEquals(count, test5.size());
    }

 
ANUTEJANUTEJ (Salesforce Developers) 
Hi Sherwin,

In test class you would want to write the scenario that would be satisfying if block and then the one that would be satisfying the else block.

I hope this helps.

Regards,
Anutej