+ Start a Discussion
Manjunath reddy 25Manjunath reddy 25 

Need help in writing test class

How to write a test class for the below class
public with sharing class GSDASDPHPEAccountTeam {
    public GSD_Account_Success_Delivery_Plan__c mainAcctID;
    public GSDASDPHPEAccountTeam(ApexPages.StandardController std)
    {
        mainAcctID = [select id, Account__c, Related_To_Account_Business_Plan__c,Related_To_Account_Business_Plan__r.primary_accountaccount__c from GSD_Account_Success_Delivery_Plan__c where id = :std.getId()];
    }
   
    public list<HPE_Account_Team__c> getHPEAccountTeam(){
        return [select Name, Account__r.Name,Name__r.Name, Phone__c, Email__c, Role__c, Role_Scope__c, Role_Scope_Description__c from HPE_Account_Team__c where Account__c = : mainAcctID.Account__c];
    }
    
}

 
Best Answer chosen by Manjunath reddy 25
Medhya MahajanMedhya Mahajan
Try this, 

 and replace your object in the below mentioned code:
@isTest
public class GSDASDPHPEAccountTeam_Test {

	Test.startTest();
	GSDASDPHPEAccountTeam gatTeam = new GSDASDPHPEAccountTeam();

	Account acc = new Account ( Name  = 'Test;
	insert acc;
	
	Related_To_Account_Business_Plan__c atabp = new Related_To_Account_Business_Plan__c(primary_accountaccount__c = acc.Id);
	insert atabp;
	
	GSD_Account_Success_Delivery_Plan__c gasdp = new GSD_Account_Success_Delivery_Plan__c(Related_To_Account_Business_Plan__c = atabp.Id);
	insert gasdp;
	
	ApexPages.StandardController sc = new ApexPages.StandardController(gasdp.Id);
	
	gatTeam.getHPEAccountTeam();
	
	Test.stopTest();
	}

Regards
Medhya Mahajan
 

All Answers

Medhya MahajanMedhya Mahajan
Hi, 

Here's the test class for you :

Please ignore syntax errors if any
@isTest
public class GSDASDPHPEAccountTeam_Test {

	GSDASDPHPEAccountTeam getHPEAccountTeam = new GSDASDPHPEAccountTeam();

	Account acc = new Account ( Name  = 'Test;
	insert acc;
	
	Related_To_Account_Business_Plan__c atabp = new Related_To_Account_Business_Plan__c(primary_accountaccount__c = acc.Id);
	insert atabp;
	
	GSD_Account_Success_Delivery_Plan__c gasdp = new GSD_Account_Success_Delivery_Plan__c(Related_To_Account_Business_Plan__c = atabp.Id);
	insert gasdp;
	
	ApexPages.StandardController sc = new ApexPages.StandardController(gasdp.Id);
	
	getHPEAccountTeam.getHPEAccountTeam();
	}

Regards
Medhya Mahajan

 
Manjunath reddy 25Manjunath reddy 25
Hi Medhya,

I am getting this error

 Invalid type: Related_To_Account_Business_Plan__c at line 11 column 53
Manjunath reddy 25Manjunath reddy 25
I tried that I am getting the error Constructor not defined: [ApexPages.StandardController].<Constructor>(Id) at line 5 column 43,
If I comment the line I am getting the error Constructor not defined: [ApexPages.StandardController].<Constructor>(Id) at line 16 
Medhya MahajanMedhya Mahajan
Try this, 

 and replace your object in the below mentioned code:
@isTest
public class GSDASDPHPEAccountTeam_Test {

	Test.startTest();
	GSDASDPHPEAccountTeam gatTeam = new GSDASDPHPEAccountTeam();

	Account acc = new Account ( Name  = 'Test;
	insert acc;
	
	Related_To_Account_Business_Plan__c atabp = new Related_To_Account_Business_Plan__c(primary_accountaccount__c = acc.Id);
	insert atabp;
	
	GSD_Account_Success_Delivery_Plan__c gasdp = new GSD_Account_Success_Delivery_Plan__c(Related_To_Account_Business_Plan__c = atabp.Id);
	insert gasdp;
	
	ApexPages.StandardController sc = new ApexPages.StandardController(gasdp.Id);
	
	gatTeam.getHPEAccountTeam();
	
	Test.stopTest();
	}

Regards
Medhya Mahajan
 
This was selected as the best answer
Manjunath reddy 25Manjunath reddy 25
Thanks for your help, finally me code is working , the code is a s below
@istest
public class GSDASDPHPEAccountTeamtest{
    @istest
    private static void GSDASDPHPEAccountTeamtestMethod(){
        Account acc = new Account ( Name = 'Test');
        
        Account_Plan__c atabp = new Account_Plan__c(Primary_AccountAccount__c= acc.Id);
        GSD_Account_Success_Delivery_Plan__c gasdp = new GSD_Account_Success_Delivery_Plan__c(Related_To_Account_Business_Plan__c = atabp.Id);
        
        
        test.startTest();
            insert acc;
            insert atabp;
            insert gasdp ;
        ApexPages.StandardController sc = new ApexPages.standardController(gasdp);
        
        GSDASDPHPEAccountTeam HpeaccountTeam = new GSDASDPHPEAccountTeam(sc);
        HpeaccountTeam.getHPEAccountTeam();
        test.stopTest();
 
    }

}

 
Manjunath reddy 25Manjunath reddy 25
sorry my working code is as below
@istest
public class GSDASDPHPEAccountTeamtest{
    @istest
    private static void GSDASDPHPEAccountTeamtestMethod(){
        test.startTest();
        Account acc = new Account ( Name = 'Test');
            insert acc;
            Account_Plan__c atabp = new Account_Plan__c(Primary_AccountAccount__c= acc.Id);
                insert atabp;
            GSD_Account_Success_Delivery_Plan__c gasdp = new GSD_Account_Success_Delivery_Plan__c(Related_To_Account_Business_Plan__c = atabp.Id);
            insert gasdp ;
            ApexPages.StandardController sc = new ApexPages.standardController(gasdp);
            
            GSDASDPHPEAccountTeam HpeaccountTeam = new GSDASDPHPEAccountTeam(sc);
            HpeaccountTeam.getHPEAccountTeam();
        test.stopTest();
 
    }

}
Medhya MahajanMedhya Mahajan
Oops I forgot to put it in a method, sorry.

Anyways please mark it as solved to help others.

 
Manjunath reddy 25Manjunath reddy 25
Thanks for helping me Medhya, I marked your answer as best answer.