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
CBNCBN 

Help me to Write a test class for below code

Hi all, 

Help me to write a test class
 
public with sharing class AccountwithActivecontactsController {

     public List<AccountInfoWrapper> AccountsWrapper {get; set;}    
    
    public List<List<AccountInfoWrapper>> listofAccountsWrapper {get; set;}
    
 /*   public AccountwithActivecontactsController()
    {
  AccountsWrapper = new List<AccountInfoWrapper>();
  listofAccountsWrapper = new List<List<AccountInfoWrapper>>();
        getAccounts();
    }  */
     
    public List<AccountInfoWrapper> getAccounts()   
    {
        AccountsWrapper = new List<AccountInfoWrapper>();
        try{
        string str = 'SELECT id,Name,Ownerid, Account.Owner.Profile.Name,Type,Total_No_Of_Contacts__c,No_Of_Opportunties__c,(SELECT id, Name, Email, Phone,  createdDate from contacts where Active__c = true order by createdDate desc LIMIT 1) FROM Account';
        for(Account a:database.query(str))
        {
            if(a.contacts.size() > 0)  {   
            AccountsWrapper.add(new AccountInfoWrapper(a, a.contacts)); 
           // AccountsWrapper.add(new AccountInfoWrapper(s.contacts));
         }   
        }   
        system.debug('***********accounts**'+AccountsWrapper);
    } 
    
        catch( QueryException e ) {
                // HANDLES EXCEPTIONS RELATED TO SOQL or SOSL STATEMENTS.
                System.debug( 'QueryException:-\n' + e.getMessage() + '\nLine Number:-\n' + e.getLineNumber() );
            }
                    return AccountsWrapper; 

            }
    
    public class AccountInfoWrapper
    {
        public Account sObj{get;set;}
        public contact con {get;set;}
        public AccountInfoWrapper(Account acc, contact con)
        {
            sObj = acc; 
            this.con = con; 
        }  
        
    }

Kindly Support and Suggest

Thanks
 
Best Answer chosen by CBN
Ajay K DubediAjay K Dubedi
Hi CBN,
You can go for below code:
 
@isTest
private class AccountwithActivecontactsControllerTest {
    @isTest static void createAccount(){
        List<Account> accList = new List<Account>();
        for(Integer i = 0; i <= 5 ; i++ ){
            Account accObj = new Account();
            accObj.Name = 'Test Account'+i;
            accObj.Total_No_Of_Contacts__c = 5;
            accObj.No_Of_Opportunties__c = 3;
            accList.add(accObj);
        }
        if(accList.size() > 0 ){
            insert accList;
            List<Contact> conList = new List<Contact>();
            for(Account accObj : accList){
                Contact conObj = New Contact();
                conObj.FirstName = 'Test';
                conObj.LastName = 'Contact'+accObj.Name;
                conObj.AccountId = accObj.Id;
                conObj.Email = 'TestAccount@gmail.com';
                conObj.Phone = '789465465';
                conObj.Active__c = true;
                conList.add(conObj);
            }
            if(conList.size()>0){
                insert conList;
            }
        }
        Test.startTest();
        AccountwithActivecontactsController newObj = new AccountwithActivecontactsController();
        newObj.getAccounts();
        Test.stopTest();
    }
}



I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks,
Ajay Dubedi

All Answers

Dinesh GopalakrishnanDinesh Gopalakrishnan
Hi CBN,

Please try the below Class and add if you need any additional fields.Kindly let me know if you face any issues.

@isTest
public class AccountwithActivecontactsController_Test
{
  @testsetup
    static void createAccount(){
        List<Account> AccountRec = new List<Account>();        
        for(Integer i = 0 ; i<10 ; i++) {
            AccountRec.add(new Account(Name = 'TestAccount'+i , Type = 'Test',Total_No_Of_Contacts__c = 5,No_Of_Opportunties__c = 2));
        } 
        insert AccountRec;
        List<Contact> ContactRec = new List<Contact>();        
        for(Integer i = 0 ; i<10 ; i++) {
            ContactRec.add(new Contact(FirstName = 'Test'+i ,LastName = 'Contact', Account = AccountRec[i].Id,Email = 'testcon@test.com',Phone = '8789544521',Active__c = true));
        } 
        insert ContactRec;
   }
    
  static testMethod void gettAccount()
  {
        System.assertEquals(10,AccountRec.size());
  
  }
}

Thanks,
Dinesh Kumar Gopalakrishnan
Ajay K DubediAjay K Dubedi
Hi CBN,
You can go for below code:
 
@isTest
private class AccountwithActivecontactsControllerTest {
    @isTest static void createAccount(){
        List<Account> accList = new List<Account>();
        for(Integer i = 0; i <= 5 ; i++ ){
            Account accObj = new Account();
            accObj.Name = 'Test Account'+i;
            accObj.Total_No_Of_Contacts__c = 5;
            accObj.No_Of_Opportunties__c = 3;
            accList.add(accObj);
        }
        if(accList.size() > 0 ){
            insert accList;
            List<Contact> conList = new List<Contact>();
            for(Account accObj : accList){
                Contact conObj = New Contact();
                conObj.FirstName = 'Test';
                conObj.LastName = 'Contact'+accObj.Name;
                conObj.AccountId = accObj.Id;
                conObj.Email = 'TestAccount@gmail.com';
                conObj.Phone = '789465465';
                conObj.Active__c = true;
                conList.add(conObj);
            }
            if(conList.size()>0){
                insert conList;
            }
        }
        Test.startTest();
        AccountwithActivecontactsController newObj = new AccountwithActivecontactsController();
        newObj.getAccounts();
        Test.stopTest();
    }
}



I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks,
Ajay Dubedi
This was selected as the best answer
CBNCBN
HI Dinesh Gopalakrishnan

I am facing on issue please review once

Error: Compile Error: Variable does not exist: AccountRec at line 20 column 32
        System.assertEquals(10,AccountRec.size());

Thank you 
CBN

 
Dinesh GopalakrishnanDinesh Gopalakrishnan
Hi CBN,
Please try this Out or try the Solution @ajay has Suggested.

@isTest
public class AccountwithActivecontactsController_Test
{
  public List<Account> AccountRec = new List<Account>(); 
  @testsetup
    static void createAccount(){
               
        for(Integer i = 0 ; i<10 ; i++) {
            AccountRec.add(new Account(Name = 'TestAccount'+i , Type = 'Test',Total_No_Of_Contacts__c = 5,No_Of_Opportunties__c = 2));
        } 
        insert AccountRec;
        List<Contact> ContactRec = new List<Contact>();        
        for(Integer i = 0 ; i<10 ; i++) {
            ContactRec.add(new Contact(FirstName = 'Test'+i ,LastName = 'Contact', AccountId = AccountRec[i].Id,Email = 'testcon@test.com',Phone = '8789544521',Active__c = true));
        } 
        insert ContactRec;
   }
    
  static testMethod void gettAccount()
  {
        System.assertEquals(10,AccountRec.size());
  
  }
}

Thanks,
Dinesh