+ Start a Discussion
dhruv aroradhruv arora 

please help to write test class for this wrapper class

public class  myy{
public integer pagenum{get;set;}
public integer fr{get;set;}
public integer to{get;set;}  

//GETTER SETTER___________________________________________________
Set<Id> i = new Set<Id>();
public integer count{get;set;}
public integer offset { get; set; }
public List<wrap> list1{get;set;}
public boolean Box{get;set;}
public boolean blo{get;set;}
public String search{get;set;}
public String search1{get;set;}
public String a1{get;set;}
public String search3{get;set;}
public integer totalRecs{get;set;}
public integer OffsetSize{get;set;}
public integer LimitSize{get;set;}
public boolean msg{get;set;}
public boolean nude{get;set;}
 public boolean check{get;set;}
 public String Ab{get;set;}
 List<SelectOption> options = new List<SelectOption>();
//CONSTRUCTOR_____________________________________________________   
  public myy(){
  fr= 0;
  pagenum = 0;
  OffsetSize= 0;
  LimitSize= 3;
  totalRecs=0;
  blo=false;
  msg=false;
  nude=true;
Box=false;
}
//ACCOUNT SELECT _______________________________________________________________
    public void abc2() {
    
count= 0;
     if(ab !='none' && ab!=null){
    
      Box=true;
      system.debug(ab);
    totalRecs = [select count() from contact where accountid =:Ab and name LIKE :'%'+ search3 +'%'];
    }
    
    else{
    system.debug(ab);
    Box=false;
    }     
    }
//SEND EMAIL_____________________________________________________________________    
     public PageReference sendmail() {
        
        for(wrap w: list1)
            {
           
            if(w.selected==true)
                {
                blo=false;
                count++;
                
                system.debug(list1.size());
                 Messaging.SingleEmailMessage message = new Messaging.SingleEmailMessage();
                 message.toAddresses = new String[]{w.con.email,w.con.email};
                     message.subject = 'sent by Contact';
                    message.plainTextBody = 'how r u:P.';
                    Messaging.SingleEmailMessage[] messages =new List<Messaging.SingleEmailMessage> {message};
                     Messaging.SendEmailResult[] results = Messaging.sendEmail(messages);   
                     ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Confirm,'Email Sent Successfully'));                                
                     }
                     else{
                     w.selected=false;
                    blo=true;
                    count= 0;
                     }
                     
            }
        return null;
    }
//WRAPPER AND CONTACTS  

     public List<SelectOption> getAccounts() {
        totalRecs = [select count() from contact where accountid =:Ab];
           options.clear();
           options.add(new selectOption('none','----None-----'));
             for(schema.account Acct: [SELECT id,name from account]){
             options.add(new selectOption(Acct.id,Acct.name));
             }
              return options;
         }
         
         public List<wrap> getnew() {
        
         if(OffsetSize!=0){
pagenum = ((OffsetSize/LimitSize)+1);

else{
pagenum = 1;
}
 fr = ((pagenum * LimitSize) - LimitSize + 1);
 
 to = (pagenum * LimitSize);
 if(to >totalRecs){
 to = totalRecs;
 }

              blo = false;
               list1=new List<wrap>();
                if(Ab!=null){
               for(contact c:[select name,email,Phone,AssistantPhone from contact where AccountId=:Ab and name LIKE :'%'+ search3 +'%']){
               if(i.contains(c.id)){
               list1.add(new wrap(c,true));}
               else{
               list1.add(new wrap(c));}
               }}
        return list1;
    }
      
         public class wrap{
        public boolean selected{get;set;}
        public Contact con{get;set;}
        public wrap(contact c1){
        con=c1;
        selected=false;
        }
        public wrap(contact c1,boolean boxs){
        con=c1;
        selected=boxs;
        }
    
    }
 //PAGINATION______________________________________________   
 public void FirstPage()
{
OffsetSize = 0;
}

public void previous()
{
for(wrap w:list1){
    if (w.selected==true){
        i.add(w.con.id);
    }else if(i.contains(w.con.id)){
        i.remove(w.con.id);
    }
}if(OffsetSize>0)
OffsetSize = OffsetSize - LimitSize;
}public void next(){

for(wrap w:list1){
    if (w.selected==true){
        i.add(w.con.id);
    }else if(i.contains(w.con.id)){
        i.remove(w.con.id);
    }
}
OffsetSize = OffsetSize + LimitSize;

}public void LastPage()
{
OffsetSize = totalrecs - math.mod(totalRecs,LimitSize);
}

public boolean getprev(){

if(OffsetSize != 0)
return false;
else
return true;
}

public boolean getnxt()
{

if((OffsetSize + LimitSize) > totalRecs)
return true;
else
return false;
}
}
Best Answer chosen by dhruv arora
Rohit K SethiRohit K Sethi
Hi 

Use below class for coverage of wrapper class :
 
@isTest
public class testing9{
    public static testMethod void unitTest1(){
        List<Account> lstAccount = new List<Account>();
        lstAccount.add(new Account(name='testAccount1'));
        lstAccount.add(new Account(name='testAccount2'));
        lstAccount.add(new Account(name='testAccount3'));
        insert lstAccount;
        List<Contact> lstContact = new List<Contact>();
        lstContact.add(new Contact(lastName='testLast1',email='test1@gmail.com',accountId=lstAccount[1].id));                
        lstContact.add(new Contact(lastName='testLast2',email='test2@gmail.com',accountId=lstAccount[1].id));
        lstContact.add(new Contact(lastName='testLast3',email='test3@gmail.com',accountId=lstAccount[1].id));                
        insert lstContact;
        
        myy m = new myy();
        m.search3 = 'test';
        m.search = 'test';
        m.Ab =  lstAccount[1].id;
        m.abc2();
        m.getAccounts();
        m.getnew();        
        m.LastPage();
        m.FirstPage();
        m.next();
        m.getprev();
        m.getnxt();
        m.previous();
        m.list1[0].selected = true;
        m.sendmail();
    }
}

Thanks.
 

All Answers

Rohit K SethiRohit K Sethi
Hi 

Use below class for coverage of wrapper class :
 
@isTest
public class testing9{
    public static testMethod void unitTest1(){
        List<Account> lstAccount = new List<Account>();
        lstAccount.add(new Account(name='testAccount1'));
        lstAccount.add(new Account(name='testAccount2'));
        lstAccount.add(new Account(name='testAccount3'));
        insert lstAccount;
        List<Contact> lstContact = new List<Contact>();
        lstContact.add(new Contact(lastName='testLast1',email='test1@gmail.com',accountId=lstAccount[1].id));                
        lstContact.add(new Contact(lastName='testLast2',email='test2@gmail.com',accountId=lstAccount[1].id));
        lstContact.add(new Contact(lastName='testLast3',email='test3@gmail.com',accountId=lstAccount[1].id));                
        insert lstContact;
        
        myy m = new myy();
        m.search3 = 'test';
        m.search = 'test';
        m.Ab =  lstAccount[1].id;
        m.abc2();
        m.getAccounts();
        m.getnew();        
        m.LastPage();
        m.FirstPage();
        m.next();
        m.getprev();
        m.getnxt();
        m.previous();
        m.list1[0].selected = true;
        m.sendmail();
    }
}

Thanks.
 
This was selected as the best answer
Srinivas SSrinivas S
Please try as below -
@isTest
private class TestMyy {
	static testMethod void testMy() {
		Account acc = new Account(Name = 'testAcc');
		insert acc;
		Contact con = new Contact(LastName = 'testCon',AccountId = acc.Id);
		insert con;
		acc.ab = String.valueOf(acc.Id);
		acc.search3 = 'test';
		myy my = new myy();		
		my.abc2();
		List<SelectOption> options = new List<SelectOption>();
		options.add(my.getAccounts());
		my.sendmail();
		my.FirstPage();
		my.previous();
		my.next();
		my.LastPage();
		Boolean b1 = my.getprev();
		Boolean b2 = my.getnxt();		
	}
}
------------
Thanks,
Srinivas
- Please mark as solution if your problem is resolved.