+ Start a Discussion
Anusha ArrojuAnusha Arroju 

wrapperclass account related contacts

Hi everyone,can someone help how to write testclass for the below code..
Thanks in Advance.

public class Displayrelatedlists {
    //1.list of wrapperclass
     public list<AccountContactWrapper> lstAccountContactWrapper { get; set; }
    public list<AccountContactWrapper> lstSelectedAccountContactWrapper { get; set; }
    //list for selecting the account records
    public list<account> selectedAccounts{get;set;} 

    public Displayrelatedlists () {
        //Allocating the memory for the list
        lstSelectedAccountContactWrapper = new list<AccountContactWrapper>();
        if(lstAccountContactWrapper == null) {
            //allocate memory for the wrapperlist
            lstAccountContactWrapper = new list<AccountContactWrapper>();
            for(account a:[select id,name,phone,(select id,name from contacts)from account limit 10]) {
                lstAccountContactWrapper.add(new AccountContactWrapper(a));
            }
        }
    }
 
    public void ProcessSelected() {
        lstSelectedAccountContactWrapper =new list<AccountContactWrapper>();
        selectedAccounts =new list<Account>();
        for(AccountContactWrapper wrapobj:lstAccountContactWrapper){
            if(wrapobj.isSelected==true) {
                selectedAccounts.add(wrapobj.acc);
            } 
        }
       
        for(Account acc:[select id,name,phone,(select id,name from contacts) from account where id in:selectedAccounts]) {
            lstSelectedAccountContactWrapper.add(new AccountContactWrapper(acc)); 
        }
             
    }
    public class AccountContactWrapper {
 
        public Account acc {get;set;}
        public boolean isSelected {get;set;}
   
        public AccountContactWrapper(account a) {
            acc = a;
            isselected=false;
        }
    }
}
Best Answer chosen by Anusha Arroju
Maharajan CMaharajan C
Hi Anusha,

Please try the below test class:
 
@isTest
public class DisplayrelatedlistsTest {
    @isTest static void testDisplayrelatedlistsMethod(){

        // Add if there is any other fields are required for Account Create
        Account acc = new Account(Name = 'Test Account');
        insert acc;
        
        // Add if there is any other fields are required for Account Create
        List<Contact> conlist = new List<Contact>();
        conlist.add(new contact(lastname = 'Test Con', accountId = acc.Id));
        conlist.add(new contact(lastname = 'Test Con1', accountId = acc.Id));
        
        insert conlist;
        
        Displayrelatedlists disp = new Displayrelatedlists();
        
        for(Displayrelatedlists.AccountContactWrapper a : disp.lstAccountContactWrapper){
            a.isSelected = true;
        }
        
        Test.startTest();
        disp.ProcessSelected();
        Test.stopTest();
        
    }
}

Thanks,
Maharajan.C

All Answers

Maharajan CMaharajan C
Hi Anusha,

Please try the below test class:
 
@isTest
public class DisplayrelatedlistsTest {
    @isTest static void testDisplayrelatedlistsMethod(){

        // Add if there is any other fields are required for Account Create
        Account acc = new Account(Name = 'Test Account');
        insert acc;
        
        // Add if there is any other fields are required for Account Create
        List<Contact> conlist = new List<Contact>();
        conlist.add(new contact(lastname = 'Test Con', accountId = acc.Id));
        conlist.add(new contact(lastname = 'Test Con1', accountId = acc.Id));
        
        insert conlist;
        
        Displayrelatedlists disp = new Displayrelatedlists();
        
        for(Displayrelatedlists.AccountContactWrapper a : disp.lstAccountContactWrapper){
            a.isSelected = true;
        }
        
        Test.startTest();
        disp.ProcessSelected();
        Test.stopTest();
        
    }
}

Thanks,
Maharajan.C
This was selected as the best answer
Anusha ArrojuAnusha Arroju
Thanks a lot maharajan.
pkin pumpkin pum
My new addiction, I really love it. slope unblocked (https://slopeunblocked.online) - 1v1 lol (https://1v1lol.online