You need to sign in to do that
Don't have an account?

Help me to Write a test class for below code
Hi all,
Help me to write a test class
Kindly Support and Suggest
Thanks
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
You can go for below code:
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
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
You can go for below code:
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi
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