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
sumit dsumit d 

code coverage issue in batch test class creating child records

Hi All,
 i created the batch given below:-
 //Batch to create FBGFamily junction records for Account having same name
public class BatchFBGFamilies implements Database.Batchable<sObject> {
    //run method to execute batch on one record
    public static void run( Set<Id> AccIds ) {
        List<Account> accountRecords =  [Select id, Name  
                                         From Account
                                         Where Id IN:  AccIds ];
        executeHelper( accountRecords );                  
    }
    //Start method to get All Account which is exetuing batch
    public Database.QueryLocator start(Database.BatchableContext BC){
        String query = 'Select Id, Name ' +
            'FROM Account';
        return Database.getQueryLocator(query);
    }
    
    public void execute(Database.BatchableContext BC, List<Account> accounts){
        executeHelper( accounts );
    }
    
    public void finish(Database.BatchableContext BC){
    }
    
    //method to perform record creation of junction object for Account having same name
    public static void executeHelper( List<Account> accounts ) {
        //set to get Account having duplicate name
        Set<String> duplicateNames = new Set<String>();
        for (Account account : accounts)
        {
            duplicateNames.add(account.Name);
        }
        
        
        //map to get Account name and list of AccountIds having same name
        Map<String, List<Account>> mapDuplicateAccNameToIds = new Map<String, List<Account>>();
        for(Account acc : [SELECT id,name FROM Account  where Name IN: duplicateNames AND ID NOT IN : accounts]) {
            if(mapDuplicateAccNameToIds.containsKey(acc.Name)) {
                List<Account> ListDuplicates = mapDuplicateAccNameToIds.get(acc.Name);
                ListDuplicates.add(acc);
                //System.debug('ListDuplicates'+ListDuplicates);
                mapDuplicateAccNameToIds.put(acc.Name, ListDuplicates);
            } else {
                mapDuplicateAccNameToIds.put(acc.Name, new List<Account> { acc });
            }
        }
        System.debug('mapDuplicateAccNameToIds'+mapDuplicateAccNameToIds);
        Map<String,FBG_Family__c> mapAlreadyexistFamily = new Map<String,FBG_Family__c>();
        for(FBG_Family__c existFamily : [select id, Name, Sister_Company__c, Sister_Company2__c FROM FBG_Family__c ]){
            String key = String.valueOf(existFamily.Sister_Company__c) + String.valueOf(existFamily.Sister_Company2__c);
            mapAlreadyexistFamily.put(key,existFamily);

        }
        //System.debug('Already exit'+mapAlreadyexistFamily);
        
        //creating junction object records
        List<FBG_Family__c> ListFBGFamily = new List<FBG_Family__c>();
        if(mapDuplicateAccNameToIds.Size() > 0){
            for (Account acc : accounts)
            {
                if( mapDuplicateAccNameToIds.containsKey(acc.Name) ){
                    System.debug('account'+accounts);
                    List<Account> DuplicateAccount = mapDuplicateAccNameToIds.get(acc.Name);
                    //System.debug('DuplicateAccount'+DuplicateAccount);
                    If(DuplicateAccount.Size() > 0) {
                        for(Account Acc1 : DuplicateAccount ){
                            FBG_Family__c Family =  new FBG_Family__c();
                            Family.Sister_Company__c =  acc.id;
                            if( acc.Id != Acc1.Id){
                                Family.Sister_Company2__c = Acc1.id;
                                String key = String.valueOf(Family.Sister_Company__c) + String.valueOf(Family.Sister_Company2__c);
                                if(!mapAlreadyexistFamily.containsKey(key)){
                                    ListFBGFamily.add(Family); 

                                }
                            } 
                        }
                    }   
                }
                
                system.debug('ListFBGFamily'+ListFBGFamily);
                
            }
        }
        if( ListFBGFamily.Size() > 0 ){
            FBGFamilyTriggerHelper.runTrigger = FALSE;
            Insert ListFBGFamily;
        }
    }
}
i created the test class for it but its not giving me the coverage.
my test  class is given below:-
@istest
public class BatchFBGFamiliesTest {
    Static testmethod void creatingFamilyRecordTest(){
        
        Account acc = new Account();
        acc.Name = 'Test18';
        insert acc;
        Account acc1 = new Account();
        acc1.Name = 'Test18';
        insert acc1;
        Account acc2 = new Account();
        acc2.Name = 'Test18';
        insert acc2;
        test.startTest();
        BatchFBGFamilies obj = new BatchFBGFamilies();
        database.executeBatch(obj);
        test.stopTest();
        List<FBG_Family__c> ListFamily = [SELECT id, Name
                                          FROM FBG_Family__c
                                         ];
        List<FBG_Family__c> ListFamilyHavingFirstAccount = [SELECT id, Name
                                                            FROM FBG_Family__c
                                                            WHERE Sister_Company__c =: acc.id 
                                                            OR Sister_Company2__c =: acc.id
                                                           ];
        List<FBG_Family__c> ListFamilyHavingSecondAccount = [SELECT id, Name
                                                             FROM FBG_Family__c
                                                             WHERE Sister_Company__c =: acc1.id 
                                                             OR Sister_Company2__c =: acc1.id
                                                            ];
        List<FBG_Family__c> ListFamilyHavingThirdAccount = [SELECT id, Name
                                                            FROM FBG_Family__c
                                                            WHERE Sister_Company__c =: acc2.id 
                                                            OR Sister_Company2__c =: acc2.id
                                                           ];
        List<FBG_Family__c> ListFamilyFirstAccountAsCompany1 = [SELECT id, Name
                                                                FROM FBG_Family__c
                                                                WHERE Sister_Company__c =: acc.id 
                                                               ];
        System.assertEquals(6, ListFamily.Size());
        System.assertEquals(4, ListFamilyHavingFirstAccount.Size());
        System.assertEquals(4, ListFamilyHavingSecondAccount.Size());
        System.assertEquals(4, ListFamilyHavingThirdAccount.Size());
        System.assertEquals(2, ListFamilyFirstAccountAsCompany1.Size());
        
        
    }
    static testmethod void RunMethodTest(){
        Account acc = new Account();
        acc.Name = 'Test18';
        insert acc;
        BatchFBGFamilies.run( new Set<Id>{ acc.Id });
    }
} how to cover the remaining part?bold lines are not covering
Any suggestions?
Best Answer chosen by sumit d
Ajay K DubediAjay K Dubedi
Hi Sumit,
In your query this condition never happens because the record that is inside the duplicateNames set  it is always inside the accounts list according to your logic:
[SELECT id, name FROM Account  where Name IN: duplicateNames AND ID NOT IN: accounts]
So change your logic to find duplicate records.
and if you take query like:
[SELECT id, name FROM Account  where Name IN: duplicateNames]
Then test class for this:
@isTest
private class BatchFBGFamilies_test {
    
    @isTest static void testRun() {
        Account acc = new Account();
        acc.Name = 'Test1';
        insert acc;
        
        BatchFBGFamilies.run( new Set<Id> { acc.Id });
        
    }
    @isTest static void testBatch() {
        List<Account> accList = new List<Account>();
        Account acc = new Account();
        acc.Name = 'Test12';
        accList.add(acc);
        
        Account acc1 = new Account();
        acc1.Name = 'Test18';
        accList.add(acc1);
        
        Account acc2 = new Account();
        acc2.Name = 'Test18';
        accList.add(acc2);
        
        insert accList;
        
        FBG_Family__c obj = new FBG_Family__c();
        obj.Sister_Company__c = acc.Id;
        obj.Sister_Company2__c = acc1.Id;
        insert obj;
        
        Test.startTest();
        BatchFBGFamilies x = new BatchFBGFamilies();
        database.executeBatch(x);
        Test.stopTest();
        
    }
}
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks,
Ajay Dubedi

All Answers

Ajay K DubediAjay K Dubedi
Hi Sumit,
In your query this condition never happens because the record that is inside the duplicateNames set  it is always inside the accounts list according to your logic:
[SELECT id, name FROM Account  where Name IN: duplicateNames AND ID NOT IN: accounts]
So change your logic to find duplicate records.
and if you take query like:
[SELECT id, name FROM Account  where Name IN: duplicateNames]
Then test class for this:
@isTest
private class BatchFBGFamilies_test {
    
    @isTest static void testRun() {
        Account acc = new Account();
        acc.Name = 'Test1';
        insert acc;
        
        BatchFBGFamilies.run( new Set<Id> { acc.Id });
        
    }
    @isTest static void testBatch() {
        List<Account> accList = new List<Account>();
        Account acc = new Account();
        acc.Name = 'Test12';
        accList.add(acc);
        
        Account acc1 = new Account();
        acc1.Name = 'Test18';
        accList.add(acc1);
        
        Account acc2 = new Account();
        acc2.Name = 'Test18';
        accList.add(acc2);
        
        insert accList;
        
        FBG_Family__c obj = new FBG_Family__c();
        obj.Sister_Company__c = acc.Id;
        obj.Sister_Company2__c = acc1.Id;
        insert obj;
        
        Test.startTest();
        BatchFBGFamilies x = new BatchFBGFamilies();
        database.executeBatch(x);
        Test.stopTest();
        
    }
}
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks,
Ajay Dubedi
This was selected as the best answer
sumit dsumit d
Hi Ajay,
           Thanks for your reply. i tried as you said . in this query which i written its giving me the other Accounts which has duplicate name and not that account on which the batch is running like i have threee accounts with same name than if batch is running on one account than In the Map(mapDuplicateAccNameToIds) its giving me the name as key and other two duplicate account as value.
how to cover this in test class?
Any suugestions?
@istest
public class BatchFBGFamiliesTest {
    Static testmethod void creatingFamilyRecordTest(){
         List<Account> accList = new List<Account>();
        Account acc = new Account();
        acc.Name = 'June12';
        accList.add(acc);
        
        Account acc1 = new Account();
        acc1.Name = 'June12';
        accList.add(acc1);
        
        Account acc2 = new Account();
        acc2.Name = 'June12';
        accList.add(acc2);
        
        insert accList;
        
        FBG_Family__c obj = new FBG_Family__c();
        obj.Sister_Company__c = acc.Id;
        obj.Sister_Company2__c = acc1.Id;
        insert obj;
        
        Test.startTest();
        BatchFBGFamilies x = new BatchFBGFamilies();
        database.executeBatch(x);
        Test.stopTest();
        List<FBG_Family__c> ListFamily = [SELECT id, Name
                                          FROM FBG_Family__c
                                         ];
        List<FBG_Family__c> ListFamilyHavingFirstAccount = [SELECT id, Name
                                                            FROM FBG_Family__c
                                                            WHERE Sister_Company__c =: acc.id 
                                                            OR Sister_Company2__c =: acc.id
                                                           ];
        List<FBG_Family__c> ListFamilyHavingSecondAccount = [SELECT id, Name
                                                             FROM FBG_Family__c
                                                             WHERE Sister_Company__c =: acc1.id 
                                                             OR Sister_Company2__c =: acc1.id
                                                            ];
        List<FBG_Family__c> ListFamilyHavingThirdAccount = [SELECT id, Name
                                                            FROM FBG_Family__c
                                                            WHERE Sister_Company__c =: acc2.id 
                                                            OR Sister_Company2__c =: acc2.id
                                                           ];
        List<FBG_Family__c> ListFamilyFirstAccountAsCompany1 = [SELECT id, Name
                                                                FROM FBG_Family__c
                                                                WHERE Sister_Company__c =: acc.id 
                                                               ];
        System.assertEquals(6, ListFamily.Size());
        System.assertEquals(4, ListFamilyHavingFirstAccount.Size());
        System.assertEquals(4, ListFamilyHavingSecondAccount.Size());
        System.assertEquals(4, ListFamilyHavingThirdAccount.Size());
        System.assertEquals(2, ListFamilyFirstAccountAsCompany1.Size());
        
        
    }
    static testmethod void RunMethodTest(){
        Account acc = new Account();
        acc.Name = 'Test18';
        insert acc;
        BatchFBGFamilies.run( new Set<Id>{ acc.Id });
    }
}