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
nilesh walkenilesh walke 

need test class for this code

//controller
public class VPContactController {
    private integer totalRecs = 0;
    private integer OffsetSize = 0;
    private integer LimitSize= 2;
    integer count= 0;
    public string accid ;
    public class cContact {
        public Contact con {get; set;}
        public Boolean selected {get; set;}
        public cContact(Contact c) {
            con = c;
            selected = false;
        }
    }
    public VPContactController  (){
        accid = apexpages.currentpage().getparameters().get('id');
        system.debug('Accid'+accid);
        totalRecs=[select count() from contact];
    }
    public List<cContact> contactList {get; set;}
    public List<cContact> getContacts(){
        if(contactList == null){
            contactList = new List<cContact>();
            for(Contact c : [select Id, Name, Email, Phone from Contact where AccountId=:accid]){
                contactList.add(new cContact(c));
            }
        }
        return contactList;
    }
    public PageReference SendMail() {
        List<Contact> selectedContacts = new List<Contact>();
        for(cContact cCon : getContacts()) {
            if(cCon.selected == true) {
                selectedContacts.add(cCon.con);
            }
        }
        for(Contact con : selectedContacts) {
            string conEmail = con.Email;
            Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
            String[] toAddresses = new String[] {conEmail};
            mail.setToAddresses(toAddresses);
            mail.setSubject('New Mail Created using VF' );
            mail.setHtmlBody('Ala ka re mail');
            Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });    
        }
        return null;
    }      
}
//////
<apex:page controller="VPContactController"  showHeader="false"   >
    <apex:form >
        <apex:pageBlock >         
            <apex:pageBlockButtons location="top"/>               
            <apex:pageBlockTable value="{!contacts}" var="c" id="table">
                <apex:column headerValue="Action" >
                    <apex:inputCheckbox value="{!c.selected}"/>
                </apex:column>
                <apex:column value="{!c.con.Name}" />
                <apex:column value="{!c.con.Email}" />
                <apex:column value="{!c.con.Phone}" />
            </apex:pageBlockTable>
            <apex:pageblockButtons location="top" >
                <apex:commandButton value="Send Email" action="{!SendMail}" rerender="table" />
            </apex:pageblockButtons>
        </apex:pageBlock>
    </apex:form>
</apex:page>
Suraj Tripathi 47Suraj Tripathi 47
Hi Nilesh,
 
"Try this it will help you."
@isTest
public class VPContactController_Test {
    @testSetup
    static void setUp(){
        try{
            Contact con=new Contact();
            con.LastName='Contact1';
            con.Email='n@gmail.com';
            insert con;
        }catch(Exception e)
        {
            System.debug('Exception due to---->: '+e.getCause());
        }
    }
    @isTest
    static void getContacts_Test()
    { 
        try
        {
            Contact con1=[SELECT Id,Email FROM Contact LIMIT 1];
            Test.startTest();
            VPContactController v=new VPContactController();
            v.getContacts();
            Test.stopTest();
        }catch(Exception e)
        {
            System.debug('Exception due to---->: '+e.getCause());
        }
    }
    @isTest
    static void SendMail_Test(){
        try{
            Contact con1=[SELECT Id,Email FROM Contact LIMIT 1];
            Test.startTest();
            VPContactController v=new VPContactController();
            v.SendMail();
            Test.stopTest();
            
        }catch(Exception e)
        {
            System.debug('Exception due to---->: '+e.getCause());
        }
        
    }
    
}
If you find your Solution then mark this as the best answer. 

Thank you!

Regards 
Suraj Tripathi
nilesh walkenilesh walke
hi  suraj it is giving only 8% ! code coverage!
Suraj Tripathi 47Suraj Tripathi 47
Hi Nilesh,
   This code is giving 75% coverage.Which is the minimum criteria of test class.
User-added image
If you find your Solution then mark this as the best answer.
Thank you!
Regards
Suraj Tripathi
 
nilesh walkenilesh walke
hi suraj
thanks for that actully i just modifyed that codes its just like:
public class VPContactController {
    private String recordId;   
    private List<ContactWrapper> wconlist; 
    public VPContactController(){
        if(Test.isRunningTest()){
            Account obj = [SELECT Id, Name FROM Account LIMIT 1];
            recordId = obj.Id;    
        }else{
            recordId = ApexPages.currentPage().getParameters().get('id');
        }  
    }
    //Paging  
    public ApexPages.StandardSetController setCon {
        get {
            if(setCon == null) {
                setCon = new ApexPages.StandardSetController(Database.getQueryLocator(
                    
                    [SELECT Id, Name, Phone,Email FROM Contact WHERE AccountId=:recordId]));
            }
            return setCon;
        }
        set;
    }
    public List<ContactWrapper> getContacts() {
        wconlist = new List<ContactWrapper>();
        setCon.setpagesize(5);
        List<Contact> cl = (List<Contact>) setCon.getRecords();
        for(Contact c:cl){
            wconlist.add(new ContactWrapper(c));
        }
        return wconlist;
    }
    public PageReference SendMail(){
        List<Contact> selectedContacts = new List<Contact>();
        System.debug('==>Inside sendEmail() '+ wconlist);
        for(ContactWrapper c: wconlist) {
            if(c.isSelected == true) {
                System.debug('==>CONTACTwRAPERiSsELECTED '+C.isSelected);
                selectedContacts.add(c.con);
            }
        }
        System.debug('toaddresses==>'+selectedContacts);
        String []toAddresses = new List<String>();
        
        for(Contact con : selectedContacts) {
            toAddresses.add(con.Email);           
        }
        System.debug('toaddresses==>'+toAddresses);
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        mail.setToAddresses(toAddresses);
        mail.setSubject('Account related contacts' );
        mail.setHtmlBody('thanks for contacts');
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });   
        return null;
    }  
    @TestVisible
    class ContactWrapper{
        public Contact con{get;set;}
        public boolean isSelected{get;set;}
        public ContactWrapper(Contact c){
            this.con = c;
            this.isSelected = false;
        }
    }
}
vp
<apex:page controller="VPContactController"  showHeader="false"   >
 <Apex:form >
  <apex:pageBlock id="contacts">
            <Apex:pageblockSection title="Contacts" columns="1" >
                 <apex:pageBlockTable value="{!contacts}" var="contact" id="table">
                      <apex:column >
                       <apex:inputCheckbox label="Select" value="{!contact.isSelected}"/>
                    </apex:column>
                    <Apex:column headervalue="Contact id">
                        <Apex:outputField value="{!contact.con.Id}"/>
                    </Apex:column>i
                    <Apex:column headervalue="Contact name">
                        <Apex:outputField value="{!contact.con.Name}" />
                    </Apex:column>
                    
                    <Apex:column headervalue="Phone">
                        <Apex:outputField value="{!contact.con.Phone}" />
                    </Apex:column>
                     
                    <Apex:column headervalue="Email">
                        <Apex:outputField value="{!contact.con.Email}" />
                    </Apex:column>
                </apex:pageBlockTable>
   
            </Apex:pageblockSection>
                
<apex:commandButton rendered="{!setCon.hasPrevious}" value="Previous" action="{!setCon.previous}" reRender="contacts" />
<apex:commandButton rendered="{!setCon.hasNext}" value="Next" action="{!setCon.next}" reRender="contacts" />
<apex:outputText rendered="{!(setCon.pageNumber * setCon.pageSize) < setCon.ResultSize}" value="{!setCon.pageNumber * setCon.pageSize} Of {!setCon.ResultSize}"></apex:outputText>
<apex:outputText rendered="{!(setCon.pageNumber * setCon.pageSize) >= setCon.ResultSize}" value="{!setCon.ResultSize} Of {!setCon.ResultSize}"></apex:outputText>....
          
        </apex:pageBlock>
    <apex:commandButton value="Send Email" action="{!SendMail}" reRender="table" />   
 </Apex:form>
   </apex:page>
t


thanks in advance