+ Start a Discussion
rupesh ranjanrupesh ranjan 

Any one can write TEST class for below code

I want to write test  class for below code

code is for selected contact which is stored in wrapper class(wrapContact )
 
Public  class sendmailexample{ 
public List<wrapContact> wrapContactList {get; set;}
    public List<Contact> selectedContacts{get;set;}
    Public String selectId {get;set;}
    public sendmailexample(){
        body = apexpages.currentpage().getparameters().get('att');
     
            if(wrapContactList == null) {
            wrapContactList= new List<wrapContact>();
            for(Contact a: [SELECT  Name,FirstName,LastName,Email,Title FROM Contact ORDER by Name ASC]) {
                wrapContactList.add(new wrapContact(a));
           }
        }
        
       
        body = '';         
       to = EmailCon; 
    }         
    public void processSelected() {
        AddRecep = '';
        selectedContacts = new List<Contact>();
        for(wrapContact wrapContactObj : wrapContactList) {
            if(wrapContactObj.selected == true) {
                selectedContacts.add(wrapContactObj.acc);
                if (AddRecep == '') {
                    AddRecep = wrapContactObj.acc.Email;
                } else {
                    AddRecep += ';' + wrapContactObj.acc.Email ;
                }
            }
        }
    }
    public class wrapContact {
        public Contact acc {get; set;}
        public Boolean selected {get; set;}
        public wrapContact (Contact a) {
            acc = a;
            selected = false;
        }
    }   
}

 
Mahesh DMahesh D
Hi Rupesh,

Please find the below Test Class:
 
@isTest
public class SendmailexampleTest{  
	static testMethod void testProcessSelected(){
		Account acc = new Account();
        acc.Name = 'Acc3';
        insert acc;         

        Contact con = new Contact();
        con.FirstName = 'Todda';
        con.LastName = 'Wilmss';
        con.AccountID = acc.Id;
        con.Email = 'twilms@verisign.com';
        con.MobilePhone = '918527116723';
        insert con;
       
        sendmailexample sme = new sendmailexample();
        sme.AddRecep='twilms@verisign.com';
        sme.to ='twilms@verisign.conm';
        sme.EmailCon ='twilms@verisign.conm'; 
        sme.Email ='twilms@verisign.conm';
        sme.body='abc';
        sme.subject='abc';
        
		for(sendmailexample.wrapContact wrapContactObj : sme.wrapContactList) {
			wrapContactObj.selected = true;
		}
		sme.processSelected();
		System.assertEquals(sme.AddRecep, 'twilms@verisign.com;twilms@verisign.com');
	}  
}

Please do let me know if it helps you.

Regards,
Mahesh