+ Start a Discussion
Ido Greenbaum 10Ido Greenbaum 10 

Apex tests for custom 'CaseEmailExtension'

Hi Commnunity, 

I was able to complete a customized 'CaseEmailExtension' code solution in my SandBox, which queries a Case EmailMessages, and output these to a VisualForce Email Template.
I am struggling with adding the Tests, and would be glad to receive some help.
Below is the VisualForce Component:
<apex:component controller="CaseEmailExtension" access="global">
    <apex:attribute name="caseId" type="Id"  description="Case Id" assignTo="{!csId}"/>    
            <apex:repeat value="{!sortEmails}" var="email">
    <apex:outputText value="{0,date,dd'.'MM'.'yyyy HH:mm:ss z}" style="font-weight:bold;font-style:italic;font-size:15px;float:left">
                    <br><apex:param value="{!email.MessageDate}" /></br></apex:outputText>
                    <br>From: &nbsp; <apex:outputText value="{!email.FromAddress}"/></br>
                    To: &nbsp; <apex:outputText value="{!email.ToAddress}"/>
                    <apex:outputPanel rendered="{!email.CcAddress!=null}" >
                    <br>Cc: &nbsp; <apex:outputText value="{!email.CcAddress}"/></br></apex:outputPanel>
                    <apex:outputPanel rendered="{!email.BccAddress!=null}" >
                    Bcc: &nbsp; <apex:outputText value="{!email.BccAddress}"/></apex:outputPanel>
                    <br>Subject: &nbsp; <apex:outputText value="{!email.Subject}"/></br>
                    <br><apex:outputText value="{!email.HtmlBody}" escape="false"/></br>
____________________________________________
        </apex:repeat>
</apex:component>
And the Apex Class:
 
public with sharing class CaseEmailExtension {

public ID csId {get; set;}  // comes from assignTo on component 

public CaseEmailExtension() { }
    
private final Case currentCase;

public CaseEmailExtension(ApexPages.StandardController currentcase) {
    this.currentCase = (Case)currentcase.getRecord();
  }

  public List<EmailMessage> getSortEmails(){
  String contactEmail = [SELECT Id, ContactEmail FROM Case where Id=:csId].get(0).ContactEmail;
  contactEmail = '%'+contactEmail+'%';
    return this.csId == null
        ? new List<EmailMessage>()  // handles UI preview use case
        : [SELECT Id, FromAddress, ToAddress, BCCAddress, CcAddress, MessageDate, Subject, Incoming, HtmlBody, CreatedBy.Name   
            from EmailMessage where ParentId =: this.csId AND(FromAddress LIKE :contactEmail OR ToAddress LIKE :contactEmail OR BCCAddress LIKE :contactEmail OR CcAddress LIKE :contactEmail)
            order by MessageDate DESC ];
  }
}

Thank you.
Best Answer chosen by Ido Greenbaum 10
karthikeyan perumalkarthikeyan perumal
Hello, 

Use below code, 
 
@isTest 
Public class CaseEmailExtension_Test{

     static testMethod void EmailExtension() {
      
      Contact TestCon= new Contact();
      TestCon.LastName='Testemail';
      TestCon.Email='Test@sdad.com';
      Insert TestCon;
      
      Case Testcase= new Case();
      Testcase.Status='New';
      Testcase.Origin='Phone';
      Testcase.Contact=TestCon;
      Insert Testcase;
      
      EmailMessage TestEmail = new EmailMessage();
      TestEmail.ValidatedFromAddress='test@xyz.com';
      TestEmail.ToAddress='TestTo@xxy.com';
      TestEmail.Subject='TestSubject';
      TestEmail.Parent=Testcase;
       Test.startTest();
       
       ApexPages.StandardController sc = new ApexPages.StandardController(Testcase);
       CaseEmailExtension Objcon= new CaseEmailExtension();
       CaseEmailExtension Obj= new CaseEmailExtension(sc);
       Obj.csId= Testcase.Id;
       Obj.getSortEmails();       
      Test.stopTest();

    }
    }
Hope this will help you.
Mark Best ANSWER if its works for you, 

Thanks
karthik
 

All Answers

karthikeyan perumalkarthikeyan perumal
Hello, 

Use below code, 
 
@isTest 
Public class CaseEmailExtension_Test{

     static testMethod void EmailExtension() {
      
      Contact TestCon= new Contact();
      TestCon.LastName='Testemail';
      TestCon.Email='Test@sdad.com';
      Insert TestCon;
      
      Case Testcase= new Case();
      Testcase.Status='New';
      Testcase.Origin='Phone';
      Testcase.Contact=TestCon;
      Insert Testcase;
      
      EmailMessage TestEmail = new EmailMessage();
      TestEmail.ValidatedFromAddress='test@xyz.com';
      TestEmail.ToAddress='TestTo@xxy.com';
      TestEmail.Subject='TestSubject';
      TestEmail.Parent=Testcase;
       Test.startTest();
       
       ApexPages.StandardController sc = new ApexPages.StandardController(Testcase);
       CaseEmailExtension Objcon= new CaseEmailExtension();
       CaseEmailExtension Obj= new CaseEmailExtension(sc);
       Obj.csId= Testcase.Id;
       Obj.getSortEmails();       
      Test.stopTest();

    }
    }
Hope this will help you.
Mark Best ANSWER if its works for you, 

Thanks
karthik
 
This was selected as the best answer
Ido Greenbaum 10Ido Greenbaum 10
Thank you @Karthikeyan, works perfectly.