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
Sfdc wonderSfdc wonder 

example test class for sending emails from apex

Hi,

im write one page for send emails of attachments from Lead Page,here user enter email address and select attachments and finally click 'send' button.
im succeed with my vfpage and Apex,but in my test class
im getting 'Methods defined as TestMethod do not support Web service callouts, test skipped' in my test class.
any one help me.
give me some example test class for sending emails from apex
Ankit Gupta@ DeveloperAnkit Gupta@ Developer
Hi,

Please post your apex class with test class.




Sfdc wonderSfdc wonder
hi,

<apex:page standardController="Lead" extensions="checkbox_Class" showChat="false" showheader="false" sidebar="false" standardStylesheets="false">
       <apex:form id="form1">
         <apex:outputPanel id="Opp1" rendered="{!render1}">
            <apex:commandButton value="Send Pictures" action="{!SendAttachment}"/><br/><br/>
        
                <table width="20%" cellspacing="1px">
                    <tr>
                        <apex:repeat value="{!wrapperList}" var="wrap">
                            <td width="20px">
                            <center><img src="https://c.ap1.content.force.com/servlet/servlet.FileDownload?file={!wrap.wrapAttachfileId}" height="100" width="100"></img><br/>
                            <apex:inputCheckbox value="{!wrap.isSelected }"/></center>
                            </td>
                         </apex:repeat>
                    </tr>
                </table>
            </apex:outputPanel>
         
            <apex:outputPanel id="Opp2" rendered="{!render2}">
                To<label style="color:RED">*</label>:&nbsp;&nbsp;&nbsp;
                <apex:inputText id="emailName" value="{!EmailName}" required="true" size="50"/>&nbsp;&nbsp;&nbsp;
                Email<label style="color:RED">*</label>:&nbsp;&nbsp;&nbsp;
                <apex:inputText id="emailInput" value="{!EmailAddress}" required="true" size="50"/><br/><br/>
             
                <apex:repeat value="{!selectedAttachment}" var="wrap1">
                    <img src="https://c.ap1.content.force.com/servlet/servlet.FileDownload?file={!wrap1}" height="100" width="100"></img>
                 </apex:repeat><br/>
                <apex:commandButton value="Send" action="{!SendEmail}"/>
            </apex:outputPanel>
    </apex:form>
 
</apex:page>

Apex Controller:
public class checkbox_Class
{
    public String propertyId{get;set;}
    public String propertyName{get;set;}
    private final Lead propertyInstance {get;set;}
    @TestVisible public List<wrapper_SendEmails> wrapList {get; set;}
    public List<Id> selectedAttachment {get; set;}
    public String EmailAddress {get;set;}
    public String EmailName {get;set;}
    public Boolean Render1 {get; set;}
    public Boolean Render2 {get; set;}
     
    public checkbox_Class(ApexPages.standardController controller)
    {
          Render1 = true;
          propertyId = (String)controller.getRecord().id;
          propertyInstance = [SELECT id,Name FROM Lead WHERE Id=:propertyId LIMIT 1];
          propertyName = propertyInstance.Name;
    }
 
    public List<wrapper_SendEmails> getwrapperList(){
        System.debug('=='+propertyId);
        if(wrapList == null){
            wrapList = new List<wrapper_SendEmails>();
         
            for(Attachment attachItr : [SELECT id,parentId from Attachment WHERE ParentId=: propertyId ]){
                wrapList.add(new wrapper_SendEmails(attachItr.id));
            }
      }
       return wrapList;
    }
 
    public pageReference SendAttachment(){
        selectedAttachment = new List<Id>();
        System.debug('==='+wrapList);
        for(wrapper_SendEmails attItr : wrapList){
            if(attItr.isSelected == true){
                selectedAttachment.add(attItr.wrapAttachfileId);
            }
        }
        Render1 = false;
        Render2 = true;
        return null;
     }

    public void SendEmail(){
    List<String> EmailStrings = new List<String>();
    EmailStrings = EmailAddress.split(',');
  
 
    try{
       Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
       List<Messaging.Emailfileattachment> fileAttachments = new List<Messaging.Emailfileattachment>();
       for (Attachment attachItr : [SELECT id,Name,Body FROM Attachment WHERE id IN: selectedAttachment])
        {
            Messaging.Emailfileattachment efa = new Messaging.Emailfileattachment();
            efa.setFileName(attachItr.Name);
            efa.setBody(attachItr.Body);
            fileAttachments.add(efa);
        }
        mail.setFileAttachments(fileAttachments);
        System.debug('===email'+EmailAddress);
        mail.setToAddresses(EmailStrings);
        mail.setSubject(propertyName);
        mail.setHtmlBody('Dear '+ EmailName +', <br/>');
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
    }catch(Exception e){
        System.debug('====exception'+e);
    }
 
        Render1 = true;
        Render2 = false;
        for(wrapper_SendEmails wraper : wrapList)
        {
        wraper.isSelected = false;
        }
        EmailAddress = '';
        EmailName = '';
        }
  
    @TestVisible public Class wrapper_SendEmails{
        public Boolean isSelected {get;set;}
        public Id wrapAttachfileId {get;set;}
     
        public wrapper_SendEmails(Id attachfileId){
            this.isSelected = false;
            this.wrapAttachfileId = attachfileId;
        }
    }
}


Mytest class

@isTest
public class SendEmail_bb_Test
{
@isTest static void SendEmailTestMethod()
{

     Lead objlead=new Lead(LastName='test      lead',company='vs',Street='test',city='test',postalcode='test',state='ap',country='ind',phone='9948155656');
     insert objlead;
   
     System.currentPagereference().getParameters().put('id',objlead.id);
   
     Attachment objAtt = new Attachment();
     objAtt.Name = 'Test';
     objAtt.body = Blob.valueof('string');
     objAtt.ParentId = objLead.Id;
     insert objAtt;
   
     ApexPages.StandardController sc =new ApexPages.StandardController(objlead); 
     SendEmail_bb b=new SendEmail_bb(sc);
     SendEmail_bb.wrapper_SendEmails sb=new SendEmail_bb.wrapper_SendEmails(objAtt.id,objAtt.Name);
   
     List<SendEmail_bb.wrapper_SendEmails>wrapList=new List<SendEmail_bb.wrapper_SendEmails>();
     wrapList.add(new SendEmail_bb.wrapper_SendEmails(objAtt.id,objAtt.Name));
   
     b.propertyId=objLead.Id;
     b.propertyName=objlead.Name;
     b.EmailAddress='ram@gmail.com';
     b.EmailName='Ram';
     sb.isSelected=true;
     sb.attachName=objAtt.Name;
     sb.wrapAttachfileId=objAtt.id;
   
   
 
 
     b.getwrapperList();
     b.SendAttachment();
     b.SendEmail();
   




}
}
Ankit Gupta@ DeveloperAnkit Gupta@ Developer
Hi,

Try the modified test class code with 89% code coverage

@isTest
public class checkbox_Class_Test
{
@isTest static void SendEmailTestMethod()
{

     Lead objlead=new Lead(LastName='test      lead',company='vs',Street='test',city='test',postalcode='test',state='ap',country='ind',phone='9948155656');
     insert objlead;
  
     System.currentPagereference().getParameters().put('id',objlead.id);
  
     Attachment objAtt = new Attachment();
     objAtt.Name = 'Test';
     objAtt.body = Blob.valueof('string');
     objAtt.ParentId = objLead.Id;
     insert objAtt;
  
     ApexPages.StandardController sc =new ApexPages.StandardController(objlead);
     checkbox_Class b=new checkbox_Class(sc);
     checkbox_Class.wrapper_SendEmails sb=new checkbox_Class.wrapper_SendEmails(objAtt.id);

  
     List<checkbox_Class.wrapper_SendEmails>wrapList=new List<checkbox_Class.wrapper_SendEmails>();
     wrapList.add(new checkbox_Class.wrapper_SendEmails(objAtt.id));
  
     b.propertyId=objLead.Id;
     b.propertyName=objlead.Name;
     b.EmailAddress='ram@gmail.com';
     b.EmailName='Ram';
     sb.isSelected=true;
     sb.wrapAttachfileId=objAtt.id;
  
  


     b.getwrapperList();
     b.SendAttachment();
     b.SendEmail();
  
}
}
Sfdc wonderSfdc wonder
Hi,

in previous test class copy paste problem,but my test class is same as u mention.

i got 89% code coverage,but test method not pass...
im getting 'Methods defined as TestMethod do not support Web service callouts, test skipped' in my test class.