+ Start a Discussion
JessFeauJessFeau 

TemplateId test class

I have a custom controller where I have a set of conditional statements (if, else if) to set the mail.setTemplateId.  I am trying to write a test class to cover these if statements but I can't figure out how to use TemplateId for the test.
Best Answer chosen by JessFeau
Neha AggrawalNeha Aggrawal
Hi JessFeau,

I had done somthing similar.
This is excerpt of my visual force code sampleCon1.cls:
 
if(Mode=='ECS')  message.setTemplateId('00X90000000XSxV');
        if(Mode=='PDC')  message.setTemplateId('00X90000000XSxZ');
        if(Mode=='ONL')  message.setTemplateId('00X90000000XSxX');
        if(Mode=='ALL')  message.setTemplateId('00X90000000XSxT');

and this the test class:
 
@isTest
public class testsampleCon1   {
static testMethod void testsampleCon1func ()    {
  Contact c1=new Contact(LastName='test', email='test@gmail.com');
  insert c1;
  ApexPages.StandardController sc = new ApexPages.StandardController(c1);
  sampleCon1 sph = new sampleCon1(sc);
     PageReference pageRef = Page.SendEmail1;
    pageRef.getParameters().put('id', String.valueOf(c1.Id));
   Test.setCurrentPage(pageRef);
   sph.getMode();
   sph.setMode('ECS');
   sph.test();

 Let me know if this works of you.
Thanks.

All Answers

Neha AggrawalNeha Aggrawal
Hi JessFeau,

I had done somthing similar.
This is excerpt of my visual force code sampleCon1.cls:
 
if(Mode=='ECS')  message.setTemplateId('00X90000000XSxV');
        if(Mode=='PDC')  message.setTemplateId('00X90000000XSxZ');
        if(Mode=='ONL')  message.setTemplateId('00X90000000XSxX');
        if(Mode=='ALL')  message.setTemplateId('00X90000000XSxT');

and this the test class:
 
@isTest
public class testsampleCon1   {
static testMethod void testsampleCon1func ()    {
  Contact c1=new Contact(LastName='test', email='test@gmail.com');
  insert c1;
  ApexPages.StandardController sc = new ApexPages.StandardController(c1);
  sampleCon1 sph = new sampleCon1(sc);
     PageReference pageRef = Page.SendEmail1;
    pageRef.getParameters().put('id', String.valueOf(c1.Id));
   Test.setCurrentPage(pageRef);
   sph.getMode();
   sph.setMode('ECS');
   sph.test();

 Let me know if this works of you.
Thanks.
This was selected as the best answer
JessFeauJessFeau
Hi Neha,

Thanks for responding!  In the case of your "Mode", is that a string set in the apex class?  When I am trying to replicate this, I am getting the error "Compile Error: Method does not exist or incorrect signature: void setAssay(String) from the type DocumentQuoteEmailController"
Neha AggrawalNeha Aggrawal
Hi JessFeau,
Mode is a picklist field. I have a visual force page where user selects a value on the picklist field, and based on that email is sent.
Sorry, that I was unclear in the above reply.

Thanks.
 
JessFeauJessFeau
Your original answer ended up being very helpful and I was able to pass code coverage. Although I never was able to use a function like your "getMode" and "setMode" and instead used something like "sph.Mode= 'ECS';" .  Thanks again!