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
SV MSV M 

Can someone help me writing test class for the below code

Hi, I have a VF page with an input text box when I enter some string it should check whether the record exists or not. I was able to achieve the functionality but in my test class, I was not able to cover a few lines.
public class searchAndLinkRecordsToAccount {
    public String searchString{get;set;}
    public Integer count;
    public List<Account_Relation__c> accRelList{get;set;}
    public searchAndLinkRecordsToAccount(ApexPages.StandardController stdController) {
    }
    public void search() {
        List<Account_Relation__c> updatedRecordsList = new List<Account_Relation__c>();
        List<Account_Relation__c> insertedRecordsList = new List<Account_Relation__c>();
        accRelList = [SELECT Id, Name, Count__c, Latest_Submitted_Date__c 
                      FROM Account_Relation__c 
                      WHERE Account__c =:ApexPages.currentPage().getParameters().get('id') AND Name =:searchString];
        if(accRelList.size() > 0) {
            for(Account_Relation__c rel : accRelList) {
                rel.Count__c = rel.Count__c + 1;
                rel.Latest_Submitted_Date__c = Datetime.now();
                updatedRecordsList.add(rel);
                ApexPages.addMessage(new ApexPages.message(ApexPages.Severity.INFO,'Record found with the Search String'));
            }
        }
        if(accRelList.size() == 0){
            Account_Relation__c accRel = new Account_Relation__c();
            accRel.Name = searchString;
            accRel.Account__c = ApexPages.currentPage().getParameters().get('id');
            accRel.Count__c = 1;
            accRel.Latest_Submitted_Date__c = Datetime.now();
            insertedRecordsList.add(accRel);
            ApexPages.addMessage(new ApexPages.message(ApexPages.Severity.INFO,'Record inserted and sent for Approval'));
        }
        if(updatedRecordsList.size() > 0) {
            update updatedRecordsList;
        }
        if(insertedRecordsList.size() > 0) {
            insert insertedRecordsList;
        }
    }
}
//Test Class

@isTest
public class searchAndLinkRecordsToAccountTest {
    public static testMethod void method1() {
        Account acc = new Account(Name='Test Acc');
        insert acc;

        Account_Relation__c accRel1 = new Account_Relation__c(Name='Test Relation');
        insert accRel1;
        acc.AnnualRevenue = 500;
        update acc;
        
        Test.startTest();
        ApexPages.StandardController stdController = new ApexPages.StandardController(acc);
        searchAndLinkRecordsToAccount recsToAcc = new searchAndLinkRecordsToAccount(stdController);
        recsToAcc.search();
        Test.stopTest();
    }
}
I was not able to cover the highlighted lines of code. Please help. Thanks in Advance.
Pravin K YadavPravin K Yadav
Hi Sai,

There is a lookup field in the Account_Relation__c object, In your VF page controller, you used a where condition on the lookup field. And you have not to fill any value in that lookup fields so that query unable to fetch any records and its effect your code coverage. Use the below code it may helpful for you.
 
@isTest
public class searchAndLinkRecordsToAccountTest {
    public static testMethod void method1() {
        Account acc = new Account(Name='Test Acc');
        insert acc;

        Account_Relation__c accRel1 = new Account_Relation__c();
        accRel1.Name='Test Relation';
        accRel1.Account__c = acc.Id;
        insert accRel1;
        
        acc.AnnualRevenue = 500;
        update acc;
        
        Test.startTest();
        ApexPages.StandardController stdController = new ApexPages.StandardController(acc);
        searchAndLinkRecordsToAccount recsToAcc = new searchAndLinkRecordsToAccount(stdController);
        recsToAcc.search();
        Test.stopTest();
    }
}

I hope you find the above solution helpful. If it does, please mark it as Best Answer to help others too.

Thanks and Regards