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
salesforce sfdxsalesforce sfdx 

How to write the testclass for below code in Sample code in Salesforce

Hi just a small sample code .
 
public class TargetPlanController {    
        
    @AuraEnabled    
    public static List <TargetPlan__c > fetchAccts(Id recordId) {    
            
        return [SELECT Name,Account__c,No_of_Leads__c,AccountCity__c,FROM TargetPlan__c where BP_Id__c =: recordId ];
          }      
        
}

How to write the Test Class for the Above code.. please let me know.
Thanks 
Prathusha Reddy.
 
Best Answer chosen by salesforce sfdx
Shri RajShri Raj
@isTest
private class TargetPlanControllerTest {
    static testMethod void testFetchAccts() {
        // Create test data
        Account testAccount = new Account(Name='Test Account');
        insert testAccount;
        TargetPlan__c testTargetPlan = new TargetPlan__c(
            Name='Test Target Plan',
            Account__c=testAccount.Id,
            No_of_Leads__c=10,
            AccountCity__c='Test City',
            BP_Id__c='12345'
        );
        insert testTargetPlan;

        // Call the method to be tested
        Id recordId = '12345';
        List<TargetPlan__c> result = TargetPlanController.fetchAccts(recordId);

        // Verify the results
        System.assertEquals(1, result.size(), 'Expected one Target Plan record');
        System.assertEquals(testTargetPlan.Name, result[0].Name, 'Expected Target Plan Name to match');
        System.assertEquals(testTargetPlan.Account__c, result[0].Account__c, 'Expected Account Id to match');
        System.assertEquals(testTargetPlan.No_of_Leads__c, result[0].No_of_Leads__c, 'Expected No of Leads to match');
        System.assertEquals(testTargetPlan.AccountCity__c, result[0].AccountCity__c, 'Expected Account City to match');
    }
}

 

All Answers

Shri RajShri Raj
@isTest
private class TargetPlanControllerTest {
    static testMethod void testFetchAccts() {
        // Create test data
        Account testAccount = new Account(Name='Test Account');
        insert testAccount;
        TargetPlan__c testTargetPlan = new TargetPlan__c(
            Name='Test Target Plan',
            Account__c=testAccount.Id,
            No_of_Leads__c=10,
            AccountCity__c='Test City',
            BP_Id__c='12345'
        );
        insert testTargetPlan;

        // Call the method to be tested
        Id recordId = '12345';
        List<TargetPlan__c> result = TargetPlanController.fetchAccts(recordId);

        // Verify the results
        System.assertEquals(1, result.size(), 'Expected one Target Plan record');
        System.assertEquals(testTargetPlan.Name, result[0].Name, 'Expected Target Plan Name to match');
        System.assertEquals(testTargetPlan.Account__c, result[0].Account__c, 'Expected Account Id to match');
        System.assertEquals(testTargetPlan.No_of_Leads__c, result[0].No_of_Leads__c, 'Expected No of Leads to match');
        System.assertEquals(testTargetPlan.AccountCity__c, result[0].AccountCity__c, 'Expected Account City to match');
    }
}

 
This was selected as the best answer
salesforce sfdxsalesforce sfdx
Thank you SriRaj.
Marked as the Best Answer