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
Guru 91Guru 91 

Help in writing test class?

Can anyone help with test class


public with sharing class AccountStructure extends Hierarchy
{
    /**
    * Return ObjectStructureMap to page
    * @return asm
    */
    public List<AccountHierarchyNode> getObjectStructure()
    {
        if ( currentId == null ) {
            currentId = System.currentPageReference().getParameters().get( 'id' );
        }
        
        System.assertNotEquals( currentId, null, 'sObject ID must be provided' );
        return formatObjectStructure( CurrentId );
    }

    /**
    * Query Account from top down to build the AccountHierarchyNode
    * @param currentId
    * @return asm
    */
    public List<AccountHierarchyNode> formatObjectStructure( String currentId )
    {
        List<AccountHierarchyNode> accountNodes = new List<AccountHierarchyNode>();
        return (List<AccountHierarchyNode>) generateHierarchy(accountNodes);
    }

    protected override HierarchyNode getAllNodes()
    {
        idToNode.clear();
        List<Account> accounts = new List<Account>();
        List<Id> currentParents         = new List<Id>();
        HierarchyNode topNode           = null;

        Integer level                   = 0;
        Boolean noChildren              = false;
        
        //Find highest level obejct in the structure
        currentParents.add( GetTopElement( currentId ) );

        //Loop though all children
        while ( !noChildren ){

            if( level == 0 ){
                //Change below     
                accounts = [SELECT Type, Site, ParentId, OwnerId, Name, Industry FROM Account WHERE Id IN :currentParents ORDER BY Name];
            } 
            else {
                //Change below      
                accounts = [SELECT Type, Site, ParentId, OwnerId, Name, Industry FROM Account WHERE ParentID IN :currentParents ORDER BY Name];
            }

            if( accounts.size() == 0 )
            {
                noChildren = true;
            }
            else
            {
                currentParents.clear();
                for (Account account : accounts)
                {
                    //Change below
                    HierarchyNode parentNode = idToNode.get(account.ParentId);
                    AccountHierarchyNode node = new AccountHierarchyNode(false, account);
                    idToNode.put(account.Id, node);
                    currentParents.add( account.id );

                    if (parentNode != null)
                        node.setParent(parentNode);
                    else
                        topNode = node;
                }
                
                maxLevel.add( level );
                level++;
            }
        }

        return topNode;
    }
    
    /**
    * Find the tom most element in Heirarchy  
    * @return objId
    */
    public String GetTopElement( String objId ){
        
        Boolean top = false;
        while ( !top ) {
            //Change below
            Account a = [ Select a.Id, a.ParentId From Account a where a.Id =: objId limit 1 ];
            
            if ( a.ParentID != null ) {
                objId = a.ParentID;
            }
            else {
                top = true;
            }
        }
        return objId ;
    }

    public with sharing class AccountHierarchyNode extends HierarchyNode
    {
        public Account account;
        public Account getAccount() { return account; }
        public void setAccount( Account a ) { this.account = a; }

        public AccountHierarchyNode(Boolean currentNode, Account account)
        {
            super(currentNode);
            this.account = account;
        }
    }
}
Best Answer chosen by Guru 91
Steven NsubugaSteven Nsubuga
@isTest
private class AccountStructureTest {

    @isTest static void test() {
        
        
        Account testAccount = new Account(Name='Test Account', Industry = 'Non Profit');
        insert testAccount;
        
		List<Account> accts = new List<Account>();
		for (Integer i = 1; i < 6; i++) {
			accts.add(new Account(Name='Test Account'+i, ParentId = testAccount.Id, Industry = 'Non Profit'));
		}
        insert accts;
		
		PageReference pageRef = Page.YourPage; // Your Page Name
        pageRef.getParameters().put('id', String.valueOf(testAccount.Id));
        Test.setCurrentPage(pageRef);
		
         
		AccountStructure acctStructure = new AccountStructure();
		
        System.assert(acctStructure.getObjectStructure() != null);
        
        System.assert(acctStructure.getAllNodes() != null);
        
        System.assert(acctStructure.GetTopElement(String.valueOf(accts[0].Id)) != null);
    }
    
}

All Answers

Raj VakatiRaj Vakati
try this 
@isTest
private class AccountStructureTest {

static testmethod void UnitTestMethod() {        
	System.Test.startTest();
	 
	 Profile profile1 = [Select Id from Profile where name = 'System Administrator'];
       
        
         User u = new User(
            ProfileId = profile1.Id,
            Username = 'testtermsconditions1234423@sadagshdghasd.com',
            Alias = 'batman',
            Email='testtermsconditions1234423@asdghasd.com',
            EmailEncodingKey='UTF-8',
            Firstname='Bruce',
            Lastname='Wayne',
            LanguageLocaleKey='en_US',
            LocaleSidKey='en_US',
            TimeZoneSidKey='America/Chicago');
            insert u;
			
			        System.runas(u) {

	list<account>parentlist = new list<account>();  
    list<account>childlist = new list<account>();   
    for (integer i = 0;i<10;i++)
        {
            parentlist.add(new account(name='Parent'+i));           
        }       
        
        insert(parentlist);         
        for( integer i = 0; i<10;i++)
        {
            childlist.add(new account(name = 'acc1'+i,parentid = parentlist[i].id));
        }
        insert(childlist);  
	
	ApexPages.currentPage().getParameters().put('id',childlist[0].Id);

	AccountStructure.getObjectStructure();
	
					}
	
	System.Test.stopTest();
  
}

}

 
Steven NsubugaSteven Nsubuga
@isTest
private class AccountStructureTest {

    @isTest static void test() {
        
        
        Account testAccount = new Account(Name='Test Account', Industry = 'Non Profit');
        insert testAccount;
        
		List<Account> accts = new List<Account>();
		for (Integer i = 1; i < 6; i++) {
			accts.add(new Account(Name='Test Account'+i, ParentId = testAccount.Id, Industry = 'Non Profit'));
		}
        insert accts;
		
		PageReference pageRef = Page.YourPage; // Your Page Name
        pageRef.getParameters().put('id', String.valueOf(testAccount.Id));
        Test.setCurrentPage(pageRef);
		
         
		AccountStructure acctStructure = new AccountStructure();
		
        System.assert(acctStructure.getObjectStructure() != null);
        
        System.assert(acctStructure.getAllNodes() != null);
        
        System.assert(acctStructure.GetTopElement(String.valueOf(accts[0].Id)) != null);
    }
    
}
This was selected as the best answer
Guru 91Guru 91
Thank you steven