+ Start a Discussion
Sai Ram 118Sai Ram 118 

Test Class Help?

Hi,
I written test class and i am getting 55%. Can any one help me
Class:

public class OV_BAUtility {
     public static List<String> accountList{get;set;}
     public static Map<id,Account> accMap{get;set;}
   
     public static String getAcctIds(String id){  
        system.debug('-----------******-----------------'+id);
        return quoteId(getAccountIds(id));        
    }
     public static list<id> getAccountIds(String Id){
        if(accMap == null){
            accMap= new Map<id,Account>([SELECT id,ParentId,Name from Account where ParentId=:Id]);
        }
        system.debug('Size of map :'+accMap.size());
        accountList = new List<String>();
        accountList.add(Id);
        for(Account acc : accMap.values()){
            if(acc.ParentId == Id){
                //accMap.remove(acc.id);
                accountList.add(acc.id);
                getChildAccount(acc.id,accountList);
            }
        }
        system.debug('FinalList'+accountList);
        return accountList;
    }
    private static void getChildAccount(Id parentId,List<ID> accountList){
        for(Account acc : accMap.values()){
            if(parentId == acc.ParentId){
                //    accMap.remove(acc.id);
                accountList.add(acc.id);
                getChildAccount(acc.id,accountList);
            }
        }
    }
    private static void getParentAccount(Id parentId,List<ID> accountList){
        for(Account acc : accMap.values()){
            if(parentId == acc.id){
                //      accMap.remove(acc.id);
                accountList.add(acc.id);
                if(acc.ParentId != null)
                    getParentAccount(acc.ParentId,accountList);
            }
        }
    }
    private static String quoteId(List<String> ids){
        String idStr = '' ;
        for(String str : ids)
            idStr += '\'' + str + '\',';
        
        idStr = idStr.lastIndexOf(',') > 0 ? '(' + idStr.substring(0,idStr.lastIndexOf(',')) + ')' : idStr ;
        System.debug('quoteId() :  idStr =*************===== ' + idStr);   
        return idStr;
    }
}
Test Class:

@isTest
private class OV_BADashBoardTest {
    
        Id recTypeId = Schema.SObjectType.Account.getRecordTypeInfosByName().get('xyz').getRecordTypeId();
    
        static testMethod void getTotalCountTest(){
            
        Id recTypeId = Schema.SObjectType.Account.getRecordTypeInfosByName().get('xyz').getRecordTypeId();
        Account acc=new Account();
        acc.Name = Datetime.now() + '' + math.random();
        acc.BillingCity = 'Indore';
        acc.BillingCountry = 'India';
        acc.OriginalAccountName__c = 'xyz';
        acc.TradestyleName__c = '';
        acc.Continent__c = 'Asia';
        acc.BillingState = 'MP';
        acc.RecordTypeId = recTypeId;
       // acc.ParentId=;
        insert acc;
        OV_BADashBoard.getToatlCount(acc.Id, acc.Continent__c);
       
    }

}

Thanks
Raj VakatiRaj Vakati
Class 
 
public class OV_BAUtility {
    public static List<String> accountList{get;set;}
    public static Map<id,Account> accMap{get;set;}
    
    public static String getAcctIds(String id){  
        system.debug('-----------******-----------------'+id);
        return quoteId(getAccountIds(id));        
    }
    public static list<id> getAccountIds(String Id){
        if(accMap == null){
            accMap= new Map<id,Account>([SELECT id,ParentId,Name from Account where ParentId=:Id]);
        }
        system.debug('Size of map :'+accMap.size());
        accountList = new List<String>();
        accountList.add(Id);
        for(Account acc : accMap.values()){
            if(acc.ParentId == Id){
                //accMap.remove(acc.id);
                accountList.add(acc.id);
                getChildAccount(acc.id,accountList);
            }
        }
        system.debug('FinalList'+accountList);
        return accountList;
    }
   @testvisible  private static void getChildAccount(Id parentId,List<ID> accountList){
        for(Account acc : accMap.values()){
            if(parentId == acc.ParentId){
                //    accMap.remove(acc.id);
                accountList.add(acc.id);
                getChildAccount(acc.id,accountList);
            }
        }
    }
  @testvisible  private static void getParentAccount(Id parentId,List<ID> accountList){
        for(Account acc : accMap.values()){
            if(parentId == acc.id){
                //      accMap.remove(acc.id);
                accountList.add(acc.id);
                if(acc.ParentId != null)
                    getParentAccount(acc.ParentId,accountList);
            }
        }
    }
    private static String quoteId(List<String> ids){
        String idStr = '' ;
        for(String str : ids)
            idStr += '\'' + str + '\',';
        
        idStr = idStr.lastIndexOf(',') > 0 ? '(' + idStr.substring(0,idStr.lastIndexOf(',')) + ')' : idStr ;
        System.debug('quoteId() :  idStr =*************===== ' + idStr);   
        return idStr;
    }
}

Test Class
 
@isTest
private class OV_BADashBoardTest {
    
    Id recTypeId = Schema.SObjectType.Account.getRecordTypeInfosByName().get('xyz').getRecordTypeId();
    
    static testMethod void getTotalCountTest(){
        
        // Id recTypeId = Schema.SObjectType.Account.getRecordTypeInfosByName().get('xyz').getRecordTypeId();
        Account acc=new Account();
        acc.Name = Datetime.now() + '' + math.random();
        acc.BillingCity = 'Indore';
        acc.BillingState = 'MP';
        // acc.ParentId=;
        insert acc;
        
        Account accParent=new Account();
        accParent.Name = Datetime.now() + '' + math.random();
        accParent.BillingCity = 'Indore';
        accParent.BillingState = 'MP';
        accParent.ParentId=acc.Id;
        insert accParent;
        OV_BAUtility.getAccountIds(acc.Id);
        OV_BAUtility.getAcctIds(acc.Id);
        OV_BAUtility.getChildAccount(acc.Id , new List<Id>{accParent.Id});
        OV_BAUtility.getParentAccount(acc.Id , new List<Id>{accParent.Id});
        
        OV_BAUtility.getChildAccount(NULL , new List<Id>{accParent.Id});
        OV_BAUtility.getParentAccount(NULL , new List<Id>{accParent.Id});
        
        //  OV_BADashBoard.getToatlCount(acc.Id, acc.Continent__c);
        
    }
    
}