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
dany__dany__ 

test class for wrapper

hi guys i am trying to write a test class for wrapper class
public class AccountSelectClassController {

    public list<account> selectedAccounts { get; set; }

    public list<wrapaccount>wrapAccountList { get; set; }
    public AccountSelectClassController(){
        if(wrapAccountList == null) {
            wrapAccountList = new List<wrapAccount>();
            for(Account a: [select Id, Name,BillingState, Website, Phone from Account limit 10]) {
                // As each Account is processed we create a new wrapAccount object and add it to the wrapAccountList
                wrapAccountList.add(new wrapAccount(a));
            }
        }
    }

    public void processSelected() {
       selectedAccounts = new List<Account>();
 
        for(wrapAccount wrapAccountObj : wrapAccountList) {
            if(wrapAccountObj.selected == true) {
                selectedAccounts.add(wrapAccountObj.acc);
            }
        }
    }
    public class wrapaccount
    {
      public account acc{get;set;}
      public boolean selected{get;set;}
      public wrapaccount(account a)
      {
       acc=a;
       selected=false;
      }
    }

}

can u plz guide  me  to write a test class 
@istest
public class AccountSelectClassControllertest
{
 static testmethod void wraptest()
  {
    account ac=new account(name='hari',billingcity='hyderabad',phone='9700956194');
    AccountSelectClassController.wrapaccount acw= new AccountSelectClassController.wrapaccount(ac);
     acw.compareTo(acw);
  }
}
 
Best Answer chosen by dany__
AshlekhAshlekh
 Hi,

Hope below code will help you.
@isTest
public class AccountSelectClassControllertest
{
  static void createAccounts()
  {
	 List<account> accountList = new List<Account>();
	 for(Integer i = 0 ;i<5;i++)
	 {
		accountList.add(new Account(name='Test'+i,billingcity='hyderabad'+i,phone='970095619'+i)); //here provide the value to required fields
	 }
	 insert accountList;
  }

  static testmethod void wraptest()
  {
    createAccounts();
	Test.startTest();
		AccountSelectClassController controller = new AccountSelectClassController();
		System.assertNotEquals(controller.wrapAccountList,null);
		for(AccountSelectClassController.wrapaccount x: controller.wrapAccountList)
		{
			x.selected = true;
		}
		controller.processSelected();
	Test.StopTest();	
  }
}

Please mark it as solution if it helps you.

All Answers

AshlekhAshlekh
 Hi,

Hope below code will help you.
@isTest
public class AccountSelectClassControllertest
{
  static void createAccounts()
  {
	 List<account> accountList = new List<Account>();
	 for(Integer i = 0 ;i<5;i++)
	 {
		accountList.add(new Account(name='Test'+i,billingcity='hyderabad'+i,phone='970095619'+i)); //here provide the value to required fields
	 }
	 insert accountList;
  }

  static testmethod void wraptest()
  {
    createAccounts();
	Test.startTest();
		AccountSelectClassController controller = new AccountSelectClassController();
		System.assertNotEquals(controller.wrapAccountList,null);
		for(AccountSelectClassController.wrapaccount x: controller.wrapAccountList)
		{
			x.selected = true;
		}
		controller.processSelected();
	Test.StopTest();	
  }
}

Please mark it as solution if it helps you.
This was selected as the best answer
dany__dany__
thnx  Ashlekh