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
ShannuShannu 

how to write test class for following accounts on map code

public class LightningMapCntrl {

    Public String accountIds{get;set;}
    public LightningMapCntrl(ApexPages.StandardSetController cntlr){
        List<Account>selAccounts = cntlr.getSelected(); //get selected records from account list view
        accountIds = ''; 
        for(Account acc : selAccounts){
            accountIds += acc.Id + '-'; //build list of ids string concatenated with comma                         
        }
        accountIds = accountIds.removeEnd('-'); 
        system.debug('+++ IDs : ' + accountIds) ;
    } 
    @auraEnabled
    public static List<Account> getAccountList(String accountIds){
        system.Debug('++++ accountIds : ' + accountIds);
        List<String> accountIdList = accountIds.split('-');
        return [Select Id, Name, type , BillingCity, BillingStreet,BillingPostalCode,BillingCountry, BillingState from account where Id In: accountIdList ]; 
    }
    
}
Best Answer chosen by Shannu
SwethaSwetha (Salesforce Developers) 
HI Sai,
You can use the below code to get started
@isTest
public class TestLightningMapCntrl {
    @isTest
    static void testGetAccountList() {
        // Create test accounts
        List<Account> accounts = new List<Account>();
        for (Integer i = 0; i < 5; i++) {
            Account acc = new Account(Name='Test Account ' + i);
            accounts.add(acc);
        }
        insert accounts;

        // Get the account ids
        String accountIds = '';
        for (Account acc : accounts) {
            accountIds += acc.Id + '-';
        }
        accountIds = accountIds.removeEnd('-');

        // Call the getAccountList method
        List<Account> result = LightningMapCntrl.getAccountList(accountIds);

        // Check that the returned accounts match the test accounts
        System.assertEquals(accounts.size(), result.size());
        for (Integer i = 0; i < accounts.size(); i++) {
            System.assertEquals(accounts[i].Id, result[i].Id);
        
        }
    }
}

If this information helps, please mark the answer as best. Thank you

All Answers

SwethaSwetha (Salesforce Developers) 
HI Sai,
You can use the below code to get started
@isTest
public class TestLightningMapCntrl {
    @isTest
    static void testGetAccountList() {
        // Create test accounts
        List<Account> accounts = new List<Account>();
        for (Integer i = 0; i < 5; i++) {
            Account acc = new Account(Name='Test Account ' + i);
            accounts.add(acc);
        }
        insert accounts;

        // Get the account ids
        String accountIds = '';
        for (Account acc : accounts) {
            accountIds += acc.Id + '-';
        }
        accountIds = accountIds.removeEnd('-');

        // Call the getAccountList method
        List<Account> result = LightningMapCntrl.getAccountList(accountIds);

        // Check that the returned accounts match the test accounts
        System.assertEquals(accounts.size(), result.size());
        for (Integer i = 0; i < accounts.size(); i++) {
            System.assertEquals(accounts[i].Id, result[i].Id);
        
        }
    }
}

If this information helps, please mark the answer as best. Thank you
This was selected as the best answer
Arun Kumar 1141Arun Kumar 1141

Hi Sai,

You can use the below code to cover the coverage of the given class:

@isTest
public class LightningMapTestCntrl {
    @isTest
    static void GetAccounttestList() {
        // Create test accounts
        List<Account> accList = new List<Account>();
        for (Integer i = 0; i < 5; i++) {
            Account acc = new Account(Name='Test Account ' + i);
            accList.add(acc);
        }
        insert accList;        
        ApexPages.StandardSetController controller = new ApexPages.StandardSetController(accList);        
        // Instantiate the LightningMapCntrl controller
        LightningMapCntrl controllerExtension = new LightningMapCntrl(controller);
        String accountIds = '';
        for (Account acc : accList) {
            accountIds += acc.Id + '-';
        }
        accountIds = accountIds.removeEnd('-');        
        // Call the getAccountList method
        List<Account> result = LightningMapCntrl.getAccountList(accountIds);        
        // Check that the returned accounts match the test accounts
        System.assertEquals(accList.size(), result.size());
        for (Integer i = 0; i < accList.size(); i++) {
            System.assertEquals(accList[i].Id, result[i].Id);            
        }
    }
}


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

Thanks