+ Start a Discussion
Guru 91Guru 91 

Test Class for Account Related Class?

Hi Everyone,
can you please help me with test class
public class AccntExtension {
    public List<AccHierarchy> accountList{get;set;}
    public String chumma{get;set;}
    public Map<id,Account> accMap{get;set;}
    public AccHierarchy hier;
    public AccntExtension(ApexPages.StandardController c){
    accMap= new Map<id,Account>([SELECT (SELECT Name,Market__r.Name,Type,StageName FROM Opportunities) from Account]);
        accountList = new List<AccHierarchy>();
        List<AccHierarchy> chilList = null;
        for(Account acc : accMap.values()){
            if(acc.ParentId == null){
                hier = new AccHierarchy();
                hier.setAccount(acc);
                accMap.remove(acc.id);
                //accountList=
                accountList.add(hier);
                chilList = getChildAccount(acc.id);
                hier.setChildVal(chilList);
                
            }
        }
        system.debug('FinalList'+accountList);
    }
    private List<AccHierarchy> getChildAccount(Id parentId){
        List<AccHierarchy> accList = new List<AccHierarchy>();
        for(Account acc : accMap.values()){
            if(parentId == acc.ParentId){
                accMap.remove(acc.id);
                AccHierarchy hierChd = new AccHierarchy();
                hierChd.setAccount(acc);
                
                accList.add(hierChd);
                List<AccHierarchy> chilList = null;
                chilList = getChildAccount(acc.id);
                hierChd.setChildVal(chilList);
            }
        }
        system.debug('Loop top '+accList);
        return accList;
    }
    
}
Best Answer chosen by Guru 91
Raj VakatiRaj Vakati
@isTest
private class AccntExtensionTest {

	@isTest static void test() {
		Account testAccount = new Account(Name='Test Account');
        insert testAccount;
		
		Account testAccountParent = new Account(Name='Test Account' ,ParentId=testAccount.Id);
        insert testAccountParent;
		
		Opportunity testOpportunity = new Opportunity(
            Name = 'Test Opportunity',
			AccountId = testAccount.Id,
            CloseDate = date.today().addDays(45),         
            StageName = 'Negotiating'
        );
		
		Opportunity testOpportunityp = new Opportunity(
            Name = 'Test Opportunity',
			AccountId = testAccountParent.Id,
            CloseDate = date.today().addDays(45),         
            StageName = 'Negotiating'
        );
		
		
		insert testOpportunityp;
		
		ApexPages.StandardController sc = new ApexPages.StandardController(testAccount);
		AccntExtension controller = new AccntExtension(sc);
		
		
	}
	
}

 

All Answers

Raj VakatiRaj Vakati
Try this 
 
@isTest
private class AccntExtensionTest {

	@isTest static void test() {
		Account testAccount = new Account(Name='Test Account');
        insert testAccount;
		
		Account testAccountParent = new Account(Name='Test Account' ,ParentId=testAccount.Id);
        insert testAccountParent;
		
		Opportunity testOpportunity = new Opportunity(
            Name = 'Test Opportunity',
			AccountId = testAccount.Id,
            CloseDate = date.today().addDays(45),         
            StageName = 'Negotiating',
            LocaleSidKey = 'en_US',
            EmailEncodingKey  = 'UTF-8',
            LanguageLocaleKey = 'en_US'
        );
		
		Opportunity testOpportunityp = new Opportunity(
            Name = 'Test Opportunity',
			AccountId = testAccountParent.Id,
            CloseDate = date.today().addDays(45),         
            StageName = 'Negotiating',
            LocaleSidKey = 'en_US',
            EmailEncodingKey  = 'UTF-8',
            LanguageLocaleKey = 'en_US'
        );
		
		
		insert testOpportunityp;
		
		ApexPages.StandardController sc = new ApexPages.StandardController(testAccount);
		AccntExtension controller = new AccntExtension(sc);
		controller.getChildAccount(testAccountParent.id);
		
	}
	
}

 
Guru 91Guru 91
Thank you Steven Nsubuga, Raj V

I am getting error
Error: Compile Error: Field does not exist: LocaleSidKey on Opportunity at line 8 column 39,

Error: Compile Error: Dependent class is invalid and needs recompilation:
Class AccntExtension : Invalid type: AccHierarchy at line 2 column 31
Raj VakatiRaj Vakati
Try this
 
@isTest
private class AccntExtensionTest {

	@isTest static void test() {
		Account testAccount = new Account(Name='Test Account');
        insert testAccount;
		
		Account testAccountParent = new Account(Name='Test Account' ,ParentId=testAccount.Id);
        insert testAccountParent;
		
		Opportunity testOpportunity = new Opportunity(
            Name = 'Test Opportunity',
			AccountId = testAccount.Id,
            CloseDate = date.today().addDays(45),         
            StageName = 'Negotiating'
            
        );
		
		Opportunity testOpportunityp = new Opportunity(
            Name = 'Test Opportunity',
			AccountId = testAccountParent.Id,
            CloseDate = date.today().addDays(45),         
            StageName = 'Negotiating'
        );
		
		
		insert testOpportunityp;
		
		ApexPages.StandardController sc = new ApexPages.StandardController(testAccount);
		AccntExtension controller = new AccntExtension(sc);
		controller.getChildAccount(testAccountParent.id);
		
	}
	
}

 
Raj VakatiRaj Vakati
@isTest
private class AccntExtensionTest {

	@isTest static void test() {
		Account testAccount = new Account(Name='Test Account');
        insert testAccount;
		
		Account testAccountParent = new Account(Name='Test Account' ,ParentId=testAccount.Id);
        insert testAccountParent;
		
		Opportunity testOpportunity = new Opportunity(
            Name = 'Test Opportunity',
			AccountId = testAccount.Id,
            CloseDate = date.today().addDays(45),         
            StageName = 'Negotiating'
        );
		
		Opportunity testOpportunityp = new Opportunity(
            Name = 'Test Opportunity',
			AccountId = testAccountParent.Id,
            CloseDate = date.today().addDays(45),         
            StageName = 'Negotiating'
        );
		
		
		insert testOpportunityp;
		
		ApexPages.StandardController sc = new ApexPages.StandardController(testAccount);
		AccntExtension controller = new AccntExtension(sc);
		
		
	}
	
}

 
This was selected as the best answer
Guru 91Guru 91
Hi Raj 

Error: Compile Error: Method does not exist or incorrect signature: void getChildAccount(Id) from the type AccntExtension at line 31 column 20
Guru 91Guru 91
Hi Steven
Error: Compile Error: Dependent class is invalid and needs recompilation:
Class AccntExtension : Invalid type: AccHierarchy at line 2 column 31
Raj VakatiRaj Vakati
Can you go and compile this class "AccHierarchy " .. Looks like there is an error in this class .. first fix it then run the test class 
Guru 91Guru 91
Thank you raj and Steven
I able resolve issue in AccHierarchy class
Now my class also passed