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
Ramin MohammadiRamin Mohammadi 

Need a test class

I don't understand test classes, can someone help me out.



public with sharing class whitespace{
  
    transient public String AccountID{get;set;}
    transient public String PAccountID{get;set;}
    transient public List<Account>TIPlist{get;set;}
    transient public String t{get;set;}
    transient public Map<String,List<Installed_Products__c>> IPList{get;set;}
    transient public String Account_Name {get;set;}
    transient public list<Installed_Products__c> P{get;set;}
    

    public whitespace(ApexPages.StandardController controller){
        }
        public Map<String,List<String>> IPListM{
            get{
                //Selects Account ID and Parent ID
                
                AccountID = ApexPages.currentPage().getParameters().get('id');
                PAccountID = [select ParentId from Account where ID = :AccountID].ParentId;
            
                //Queries Account info based on criteria
                if (PAccountID == null){
                    TIPList = [select Name,(select Account__c,Product_Family__c from Product_Releases_del__r ORDER BY Product_Family__c ASC) from Account a where ParentID =: AccountID];
                }
                else {
                    TIPList = [select Name,(select  Account__c,Product_Family__c from Product_Releases_del__r ORDER BY Product_Family__c ASC) from Account a  where ParentID =: PAccountID];   
                }
                //Declares Map and List
                Map<String,List<String>> IPList = new Map<String,List<String>>();
                
                for(integer j = 0; j < TIPList.size(); j++){ 
                    List <String> P = new List<String>{};
                    //sets temporary variable t, Account Name as String and clears the product list
                    P.clear();
                    t = '';
                    Account_Name = TIPList[j].Name;
                    
                
                    
                    for(Integer i = (TIPList[j].Product_Releases_del__r.size()-1) ; i >= 0; i--){
                        system.debug(TIPList[j]);
                        Installed_Products__c x = TIPList[j].Product_Releases_del__r[i];
                        
                        if(t == x.Product_Family__c){
                            }
                        else{
                            system.debug(TIPList[j].Product_Releases_del__r[i].Product_Family__c);
                            system.debug(TIPList[j]);
                            P.add(TIPList[j].Product_Releases_del__r[i].Product_Family__c);
                            t = x.Product_Family__c;
                                }
                        }
                        
                
                    
                            
                    system.debug(P);
                    system.debug(Account_Name);
                    IPList.put(Account_Name, P);
                    system.debug(IPlist);
                    }  
                return IPList;
                }

        
        
            set;
    }
}
Amit Chaudhary 8Amit Chaudhary 8
Please check below blog to learn test classes
http://amitsalesforce.blogspot.in/2015/06/best-practice-for-test-classes-sample.html

Please try below code
@isTest 
public class whitespaceTest 
{
	static testMethod void testMethod1() 
	{
		Account testAccount = new Account();
		testAccount.Name='Test Account' ;
		insert testAccount;

		Test.StartTest(); 

		PageReference pageRef = Page.AccountPlan; // Add your VF page Name here
		pageRef.getParameters().put('id', String.valueOf(testAccount.Id));
		Test.setCurrentPage(pageRef);

		ApexPages.StandardController sc = new ApexPages.StandardController(testAccount);
		whitespace testAccPlan = new whitespace(sc);
		Map<String,List<String>> tempMap testAccPlan.IPListM();
		

		Test.StopTest();
	}
	static testMethod void testMethod2() 
	{
		Account testAccountP = new Account();
		testAccountP.Name='Test Account' ;
		insert testAccountP;

		Account testAccount = new Account();
		testAccount.Name='Test Account' ;
		testAccount.ParentId = testAccountP.id;
		insert testAccount;

		Test.StartTest(); 

		PageReference pageRef = Page.AccountPlan; // Add your VF page Name here
		pageRef.getParameters().put('id', String.valueOf(testAccount.Id));
		Test.setCurrentPage(pageRef);

		ApexPages.StandardController sc = new ApexPages.StandardController(testAccount);
		whitespace testAccPlan = new whitespace(sc);
		Map<String,List<String>> tempMap testAccPlan.IPListM();
		

		Test.StopTest();
	}
}

Please let us know if this will help u

Thanks
Amit Chaudhary