+ Start a Discussion
Thomas Kayser-EichbergThomas Kayser-Eichberg 

how to test my class (render VF Pages as PDF and Attach file to record)

Hi everyone,

I am totally newbie to salesforce and apex. As I already managed to find a way to render a visualforce page to pdf and attach it by class to the record. This runs perfect in Sandbox. I dont get it to production instance due to the fact I dont even know how to test it.

Can anybody help me out generating a test class for my class?
would be so great to get it running. Thx to all!

Here is my Code:

public class quotePDFExtension {
    ApexPages.StandardController controller;
    public Quote quote {get;set;}
    public PageReference rtn;
    public quotePDFExtension(ApexPages.StandardController c){
        quote = (Quote)c.getRecord();
        rtn = c.view();
    public PageReference attachQuotePDF() {
        /* Get the page definition */
        PageReference pdfPage = Page.angebot;
        /* generate the pdf blob */
        Blob pdfBlob = pdfPage.getContent();
        /* create the attachment against the offer */
        Attachment a = new Attachment(parentId = quote.id, name=quote.Angebot_Nr__c + '.pdf', body = pdfBlob);
        /* insert the attachment */
        insert a;
        /* send the user back to the offer detail page */
        return rtn;


Dhanya NDhanya N
Hi Thomas,

Take a look into this link http://amitsalesforce.blogspot.com/2015/06/best-practice-for-test-classes-sample.html. You will get idea how to write test class for your class.

Thomas Kayser-EichbergThomas Kayser-Eichberg
Thanks Dhana N

actually I'll get a 36% Coverage which isn't enough. Maybe you or anybody else can give me a hint to get it done?

My Class
public class quotePDFExtension {
    ApexPages.StandardController controller;
    public Quote quote {get;set;}
    public PageReference rtn;
    public quotePDFExtension(ApexPages.StandardController c){
        quote = (Quote)c.getRecord();
        rtn = c.view();
    public PageReference attachQuotePDF() {
        /* Get the page definition */
        PageReference pdfPage = Page.angebot;
        /* generate the pdf blob */
        Blob pdfBlob = pdfPage.getContent();
        /* create the attachment against the offer */
        Attachment a = new Attachment(parentId = quote.id, name=quote.Angebot_Nr__c + '.pdf', body = pdfBlob);
        /* insert the attachment */
        insert a;
        /* send the user back to the offer detail page */
        return rtn;

my test Class
public class attachQuoteTestClass 
 static testMethod void testMethod1() 
 Quote testQuote = new Quote(OpportunityId = '0061x00000AN92q', Gutachtennummer__c = '999195041551', Gutachten_Datum__c = Date.today(), Angebotsdatum__c = Date.today());
 testQuote.Name='Test Quote record' ;
 insert testQuote;
 Attachment attach = new Attachment();
 attach.Name='Unit Test Attachment';
 Blob bodyBlob = Blob.valueOf('Unit Test Attachment Body');
 attach.parentId = testQuote.Id;
 insert attach;
 List<Attachment> attachments=[select id, name from Attachment where parent.id=:testQuote.id];
 System.assertEquals(1, attachments.size());
 ApexPages.StandardController sc = new ApexPages.StandardController(testQuote);
 quotePDFExtension ctr = new quotePDFExtension(sc);

  ApexPages.StandardController sc = new ApexPages.StandardController(testQuote);
  quotePDFExtension testQuotePlan = new quotePDFExtension(sc);

  PageReference pageRef = Page.angebot;
  pageRef.getParameters().put('id', String.valueOf(testQuote.Id));
  Blob b = blob.valueof('error');
  Try { b = pageRef.getContent();}
  Catch(Exception e)
  {Attachment a = new Attachment(parentId = testQuote.id, name=testQuote.Angebot_Nr__c + '.pdf', body = b);
  insert a;}

Big Thanks
Thomas Kayser-EichbergThomas Kayser-Eichberg
Please, can anybody help me?