+ Start a Discussion
Shakti Jadon 5Shakti Jadon 5 

How can we write test class for this piece of code it contains for loop and all

 Public Void setDeletedOrgUnitvalues(Map<String,String> mapAccOrgUnit){
        
            List<Account> accObj2= [Select Id,REX_Territory_Level_7__c,REX_Territory_Level_8__c,REX_Customer_Manager__c from Account where Id In (Select ACCL__Account__c From ACCL__Account_Manager__c where ACCL__Account__c NOT IN:mapAccOrgUnit.keyset())];
        for (Account acc1 :accObj2){
            acc1.REX_Territory_Level_8__c = null;
             acc1.REX_Territory_Level_7__c = null;
             acc1.REX_Customer_Manager__c  = null;
            Database.update(acc1);
        }
        
        List<Account> accObj3= [Select Id,REX_Territory_Level_7__c,REX_Territory_Level_8__c,REX_Customer_Manager__c from Account where (REX_Territory_Level_7__c <> null  OR REX_Territory_Level_8__c <> null Or REX_Customer_Manager__c <> Null) and ID NOT IN:mapAccOrgUnit.keyset()];
        for (Account acc2 :accObj3){
            acc2.REX_Territory_Level_8__c = null;
             acc2.REX_Territory_Level_7__c = null;
             acc2.REX_Customer_Manager__c  = null;
            Database.update(acc2);
        }
        
    }
Best Answer chosen by Shakti Jadon 5
kumud thakur 20kumud thakur 20
Hope below code help you.
@isTest 
    public class ControllerTestClass 
    {
        
        static Map<String,String> mapAccOrgUnit ;
        
        private static void createData(){
            // creating account for test class
            List<Account> listAccount=new List<Account>();
            Account instAccount=new Account();
            instAccount.name='test';
            instAccount.REX_Territory_Level_7__c='L1';
            instAccount.REX_Territory_Level_8__c='L1';
            instAccount.REX_Customer_Manager__c='test user';
            listAccount.add(instAccount);
            
            instAccount.name='OrgAccount;
            instAccount.REX_Territory_Level_7__c='L1';
            instAccount.REX_Territory_Level_8__c='L1';
            instAccount.REX_Customer_Manager__c='test user';
            listAccount.add(instAccount);
            insert listAccount;
            
            // creating child object data for testing
            ACCL__Account_Manager__c instACM=new ACCL__Account_Manager__c();
            instACM.name='test' // supposing name is text field if it is autonumber comment the line
            instACM.ACCL__Account__c=listAccount[0].id;
            insert instACM;
            mapAccOrgUnit=new Map<string,String>();
            mapAccOrgUnit.put(listAccount[0].id,'testString'); // map use to pass in class method contain account id as a key
            // and value- I put dummy.
            
            
        }
        private testmethod static void myUnitTest(){
            
            createData();
            Test.startTest();
            MyClass instMyClass=new MyClass(); // please create an object for your class.


            instMyClass.setDeletedOrgUnitvalues(mapAccOrgUnit);
            Test.stopTest();
            
        }
        
    }// end    

Thanks!! 
If this works pls mark as best answer.

All Answers

kumud thakur 20kumud thakur 20
Hope below code help you.
@isTest 
    public class ControllerTestClass 
    {
        
        static Map<String,String> mapAccOrgUnit ;
        
        private static void createData(){
            // creating account for test class
            List<Account> listAccount=new List<Account>();
            Account instAccount=new Account();
            instAccount.name='test';
            instAccount.REX_Territory_Level_7__c='L1';
            instAccount.REX_Territory_Level_8__c='L1';
            instAccount.REX_Customer_Manager__c='test user';
            listAccount.add(instAccount);
            
            instAccount.name='OrgAccount;
            instAccount.REX_Territory_Level_7__c='L1';
            instAccount.REX_Territory_Level_8__c='L1';
            instAccount.REX_Customer_Manager__c='test user';
            listAccount.add(instAccount);
            insert listAccount;
            
            // creating child object data for testing
            ACCL__Account_Manager__c instACM=new ACCL__Account_Manager__c();
            instACM.name='test' // supposing name is text field if it is autonumber comment the line
            instACM.ACCL__Account__c=listAccount[0].id;
            insert instACM;
            mapAccOrgUnit=new Map<string,String>();
            mapAccOrgUnit.put(listAccount[0].id,'testString'); // map use to pass in class method contain account id as a key
            // and value- I put dummy.
            
            
        }
        private testmethod static void myUnitTest(){
            
            createData();
            Test.startTest();
            MyClass instMyClass=new MyClass(); // please create an object for your class.


            instMyClass.setDeletedOrgUnitvalues(mapAccOrgUnit);
            Test.stopTest();
            
        }
        
    }// end    

Thanks!! 
If this works pls mark as best answer.
This was selected as the best answer
Shakti Jadon 5Shakti Jadon 5
Thanks 
kumud thakur 20 worked for me with some modifications.

Thanks