0

I found a way to write an apex class that attaches my quote which is generated by an vf page as a pdf to my quote object.

Writing the test class is even a bigger problem for me. I get to 36% coverage and hop you guys can help me with it to get it working.

here's 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;
        pdfPage.getParameters().put('id',quote.id);
        /* 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;
    }


}

and here is my test class

    @isTest 
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.body=bodyBlob;
 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);
*/

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

  PageReference pageRef = Page.angebot;
  pageRef.getParameters().put('id', String.valueOf(testQuote.Id));
  Test.setCurrentPage(pageRef);

  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;}

 Test.StopTest();
 }
}

Thanks a lot Thomas

2
  • Welcome to SFSE! I've linked some collections of resources we have for learning to write good unit tests that achieve code coverage. I'd encourage working through them and making some changes (for example, never hard-code an Id in a unit test; always generate required data) and then returning with a more specific issue we can assist you with.
    – David Reed
    Oct 13, 2019 at 17:46
  • Thanks David for your reply. As Is need to attach an opportunity record to my quote Id coded to do so with an existing one. shouldn't be important for my code as the opportunity isn't part. I really struggle with getting the coverage up to at least 75% and can't figure out why. all variables are testet. what is wrong with my test code? Oct 16, 2019 at 8:49

0

Browse other questions tagged .