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

Testing a simple Controller extension to a component for a Visualforce email template / page



I have a pretty simple Apex class which is an extension for a component used in a Visualforce email template.  Since there is no Visualforce page which you'd normally use to test an extension class how do I create a test class? 


I've tested several visualforce pages before with no problem, but haven't had to test one with a component, or one which is visualforce email and not a page.


 I did create a simple Visualforce page with just the controller on (see code) and used that to test from, however it gives no code coverage .


Any ideas??


Thanks in advance

Visualforce mark-up:
<apex:page standardcontroller="Contract_Review__c" showheader="false" contenttype="application/pdf"  extensions="ExtFindings" renderas="pdf"  cache="true">
<c:ContractReviewComp thisReviewId="{!}"/>


Component Mark-up:
<apex:component controller="ExtFindings" access="global">
    <apex:attribute name="thisReviewID" description="id for current record" type="ID" assignTo="{!thisReviewID}"/>
<div style="page-break-after:always;">

        <apex:DataTable value="{!KeyFindings}" var="item" cellpadding="10" rules="all" styleclass="pageblock" >

            <apex:column value="{!item.Name}" headervalue="Ref" />

            <apex:column value="{!item.Review_Area__c}" headervalue="Review Area" />
            <apex:column value="{!item.Status__c}" style="{!IF(item.Status__c=='Failed','color:red;font-weight: bold','color:green;font-weight:bold')}" headervalue="Status"/>

            <apex:column value="{!item.What_was_the_finding__c}" headervalue="What was the finding?" />
            <apex:column value="{!item.Type__c}" headervalue="Type" />
              <apex:column value="{!item.What_is_the_financial_Impact__c}" headervalue="What is the financial impact ($)?" />



Apex Class:
public class ExtFindings {
 public ID thisReviewID {get;set;}
    Public Review_findings__c [] getKeyfindings () {
        return [Select Name,Review_area__c,Status__c,What_was_the_finding__c,type__c,What_is_the_financial_Impact__c,related_to_review__c
        FROM Review_Findings__c where related_to_review__c = :thisReviewID 
        and include_in_executive_summary__c='Yes' order by Name ASC];


Best Answer chosen by Admin (Salesforce Developers) 

As an update to this I have managed to test it by creating a dedicated Visualforce page. Seems a bit overkill to create a page just to test the code used in a Visualforce email but couldnt find any other way out there.


I also found that the component creates an empty method with the same name as the controller. Since the visualforce page didnt access this I had to call the empty method directly from within the test class to get 100% code coverage.