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
huskerwendyhuskerwendy 

Test class for Visualforce Email Template

I made my first Visualforce page which is a Visualforce Email Template. I'm having a problem writing a test class with 100% coverage. Currently, I'm at 50% and it's only got to cover four lines. I'm not sure what I'm doing wrong. Here's the test class. I added the create account, opportunity and opportunity line item but it didn't change my code coverage.

@isTest
private class TestGetBloomerProductsController {

      public static testMethod void myUnitTest() {
      	Account a = new Account(Name='Testing Account', 
	    		Industry='Nonprofit', 
	    		Type = 'Client');
	    insert a;
	    
      	Opportunity oppty = new Opportunity(
      		name='wendy test',
      		Type = 'New Client',
      		StageName = 'Close/Won',
      		Reason_Won_Lost__c='Design',
      		CloseDate=System.today(),
      		AccountId=a.id);
      	insert oppty;
      	
      	OpportunityLineItem oli = new OpportunityLineItem(
      		UnitPrice = 0.00,
      		Quantity = 1,
      		PricebookEntryId = [select Id From PricebookEntry Where Name = 'Bloomerang 1,001-5,000 Records' LIMIT 1].id,
      		OpportunityId = oppty.Id);
      	
        PageReference pageRef = Page.success;
        Test.setCurrentPage(pageRef);
        getBloomerProductsController controller = new getBloomerProductsController();
    }
}

 Here's the controller class:  (only lines 4 and 7 are covered and I need to cover 10 and 11)

public class getBloomerProductsController {
 2 	  
 3 	   //a list to hold the bloomerproducts
 4 	   public List<OpportunityLineItem> BloomerangProducts {get; set;}
 5 	  
 6 	   // get the oppty id
 7 	   public id OpptyID {
 8 	   get;
 9 	   set {
 10 	   OpptyID = value;
 11 	   BloomerangProducts = [Select PricebookEntry.Name From OpportunityLineItem WHERE HasBloomerang__c = true and OpportunityID =: OpptyID];
 12 	   }
 13 	   }
 14 	  }

 

 

 Here's the component:

<apex:component id="getBloomerProducts" controller="getBloomerProductsController" access="global">	
	<apex:attribute name="OpportunityID" description="This is the Opportunity Id." type="Id" assignTo="{!OpptyID}"/> 	
 	<apex:repeat value="{!BloomerangProducts}" var="blp" >
 		<br>
	    	<apex:outputText value="Product: "/><apex:outputField value="{!blp.PricebookEntry.Name}"/>
	    </br>	                    
	</apex:repeat>	
</apex:component>

 

 

Best Answer chosen by Admin (Salesforce Developers) 
Avidev9Avidev9
try this controller.OpptyID = oppty.Id;

All Answers

Avidev9Avidev9
Try adding this at the end
controller.setOpptyID (oppty.Id);
huskerwendyhuskerwendy

Thanks for the reply. When I add that code at the end, I get this error: Save error: Method does not exist or incorrect signature: [getBloomerProductsController].setOpptyID(Id)

Avidev9Avidev9
try this controller.OpptyID = oppty.Id;
This was selected as the best answer
huskerwendyhuskerwendy

That got me to thinking and I changed it to this and it worked.

 

controller.OpptyID=oppty.id;

 

Thanks!

huskerwendyhuskerwendy

We posted at the same time. ;) Thanks so much for the help.

Avidev9Avidev9
naah I was lil faster ;)
Rebecca VandersliceRebecca Vanderslice
i'm having trouble with this too.  you created a page reference with Page.success.  is that the name of your Email Template?  i can't seem to get my test to go to the email template and then access the controller.