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
Dan Dodd 3Dan Dodd 3 

APEX testing New Case controller extension

I found this How To Show HTML Email Right On Your Case Page (http://success.salesforce.com/ideaView?id=087300000007TOe) that really works great for what we want but cannot understand how to test it.
 
public with sharing class CaseHTMLEmailController {
    private final Case caseObj;

    public String firstHTMLEmail { get
        {return getFirstHTMLEmail(); }
    }
    
    public CaseHTMLEmailController(ApexPages.StandardController stdController) {
        this.caseObj = (Case)stdController.getRecord();
    }

    public String getFirstHTMLEmail() {
        EmailMessage firstEmail = [Select HtmlBody From EmailMessage where ParentId=:caseObj.Id order by LastModifiedDate asc limit 1];
        if (firstEmail!=null) {
            return firstEmail.HtmlBody;
        }
        
        return '';
    }
}

I need the email-to-case context where the case was generated by an email and test  it pulling the data from emailMessage for this visualforce page.
Best Answer chosen by Dan Dodd 3
Maharajan CMaharajan C
Hi Dan,

Use the below test class:
 
@isTest
public class CaseHTMLEmailControllerTest {
    Static testmethod void CaseHEMtest(){
        Case cs = new case();
        cs.Status = 'New';
        cs.Origin = 'Email';
        // Add the remaining Mandatory fields for case creation.
        insert cs;
        
        EmailMessage EM = new EmailMessage();
        EM.fromaddress='test@email.com';
        EM.toAddress = 'test@test.com';
        EM.subject = 'Test Message';
        EM.parentid=cs.id;
        EM.incoming=true;
        EM.HtmlBody = 'This is the message body';
        insert EM;
        
        CaseHTMLEmailController obj = new CaseHTMLEmailController ( new ApexPages.StandardController( cs ) );  
        String str = obj.firstHTMLEmail;
    }
}

Thanks,
Maharajan.C

All Answers

Agustin BAgustin B
HI, check this links that cover how to insert a case with their related EmailMessage.
https://salesforce.stackexchange.com/questions/159544/test-coverage-of-case-and-emailmessage-triggers

If it helps you please mark this answer as correct, it may help others.
Maharajan CMaharajan C
Hi Dan,

Use the below test class:
 
@isTest
public class CaseHTMLEmailControllerTest {
    Static testmethod void CaseHEMtest(){
        Case cs = new case();
        cs.Status = 'New';
        cs.Origin = 'Email';
        // Add the remaining Mandatory fields for case creation.
        insert cs;
        
        EmailMessage EM = new EmailMessage();
        EM.fromaddress='test@email.com';
        EM.toAddress = 'test@test.com';
        EM.subject = 'Test Message';
        EM.parentid=cs.id;
        EM.incoming=true;
        EM.HtmlBody = 'This is the message body';
        insert EM;
        
        CaseHTMLEmailController obj = new CaseHTMLEmailController ( new ApexPages.StandardController( cs ) );  
        String str = obj.firstHTMLEmail;
    }
}

Thanks,
Maharajan.C
This was selected as the best answer
Dan Dodd 3Dan Dodd 3

Both answers very helpful.

Agustin B you pointed me to useful info with that link.
Maharajan.C. your code was almost drop in perfect.
Thanks to you both.
 

Dan

Jake McGillJake McGill
@Maharajan C

Instead of taking hours to figure out how to test Visualforce, I get to come here, do a quick copy+paste job and get on with my actual job instead of wrestling with SFDC's arduous requirements.

Thank you!