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
shwetha prabhakarshwetha prabhakar 

Test class for quote pdf

Can anyone help me to write the test class for following apex class

public class QuoteApexClass {
    public Id testId {get;set;}
    private Quotes__c quoteObj;
    public String renderAs { get; set; }
     
    public QuoteApexClass(ApexPages.standardController con){
        if(con.getId()!=NULL){
            testId = con.getId();
            quoteObj =  [select id,Name,Email__c,ContactId__c from Quotes__c where id =:testId];
        }
    }
    public PageReference saveQuoteAsPDFandEmail()
    {    
        PageReference pr = Page.QuoteTemplate;
        Attachment attachmnt = new Attachment();
        Blob pdfBlob;
        try {
            pdfBlob = pr.getContentAsPDF();
        }
        catch (VisualforceException e) {
            pdfBlob = Blob.valueOf('Some Text');
        }
        
        attachmnt.parentId = testId;
        attachmnt.Name = 'V - '+ system.Now() + '.pdf';
        attachmnt.body = pdfBlob;
        insert attachmnt;
        
        
        Messaging.SingleEmailMessage semail = new Messaging.SingleEmailMessage();
        Messaging.EmailFileAttachment attach = new Messaging.EmailFileAttachment();
        
        attach.setFileName(attachmnt.Name);
        attach.setBody(attachmnt.body);
        attach.setContentType('application/pdf');
        semail.setSubject('Quote template '+quoteObj.Name);
        List <String> listEmailMembers = new List<String>();
        listEmailMembers.add('chandra.s@proseraa.com');
        listEmailMembers.add(UserInfo.getUserEmail());
        semail.setToAddresses(listEmailMembers);
        semail.saveAsActivity = true;
        semail.setPlainTextBody('As Discussed over the phone.Please find the quotation attached.Also sign the document and send us back.');
        semail.setFileAttachments(new Messaging.EmailFileAttachment[] { attach });
        Messaging.SendEmailResult [] r1 = Messaging.sendEmail(new messaging.SingleEmailMessage[] {semail});
        
         if (r1[0].success) 
        {
            System.debug('The email was sent successfully.');
        } else 
        {
            System.debug('The email failed to send: ' + r1[0].errors[0].message);
        }
        
        Task tk = new Task();
        tk.ActivityDate = system.today();
        tk.Subject = 'Email has been sent with subject name as '+'Quote template '+quoteObj.Name;
        tk.Description ='Email has been sent with subject name as '+'Quote template" '+quoteObj.Name + ' " along with an attachment "'+attachmnt.Name+ ' " at '+system.now();
        tk.Status = 'completed';
        tk.WhatId = testId;
        insert tk;
        
        attachment att = new attachment();
        att.Name = 'V - '+ system.Now() + '.pdf';
        att.body = pdfBlob;
        att.ParentId = tk.Id;
        insert att;
        
        PageReference pageRef = new PageReference('https://proseraa.lightning.force.com/lightning/r/Quotes__c/'+testId+'/view');
        pageRef.setRedirect(true);
        return pageRef;  
    }
    
    public void attachPDF() {
        PageReference pdfPage = Page.QuoteTemplate;
        Attachment attach = new Attachment();
        Blob pdfBlob;
        try {
            pdfBlob = pdfPage.getContent();
        }
        catch (VisualforceException e) {
            pdfBlob = Blob.valueOf('Some Text');
        }
        
        attach.parentId = ApexPages.currentPage().getParameters().get('id');
        attach.Name = 'Letter - '+ system.Now() + ' .pdf';
        attach.body = pdfBlob;
        insert attach;
    }
    
    public Boolean getShowPrintLink() {
        return ( (renderAs == null) || ( ! renderAs.startsWith('PDF')) );
    }
   
    public PageReference print() {
        renderAs = 'PDF';
        PageReference pdfPage = Page.QuoteTemplate;
        Attachment attach = new Attachment();
        Blob pdfBlob;
        try {
            pdfBlob = pdfPage.getContentAsPDF();
        }
        catch (VisualforceException e) {
            pdfBlob = Blob.valueOf('Some Text');
        } 
        
        attach.parentId = testId;
        attach.Name = 'V - '+ system.Now() + '.pdf';
        attach.body = pdfBlob;
        insert attach;
        
        PageReference pageRef = new PageReference('https://proseraa.lightning.force.com/lightning/r/Quotes__c/'+testId+'/view');
        pageRef.setRedirect(true);
        return pageRef;  
    }
}
Maulik D ShahMaulik D Shah

Try this,

@isTest
Public class QuoteApexClassTest {
    @isTest public static void QuoteApextestMethod() {
        Opportunity opp = new Opportunity();
        opp.Name = 'TestOpp';
        opp.CloseDate = System.today() + 5;
        opp.StageName = 'Prospecting';
        Insert opp;
        
        Quote q = new Quote();
        q.Name = 'test';
        q.OpportunityId = opp.Id;
        Insert q;
        

       ApexPages.StandardController sc = new ApexPages.StandardController(q);
        PageReference pageRef = Page.QuoteTemplate;
        pageRef.getParameters().put('id', String.valueOf(q.Id));
        Test.setCurrentPage(pageRef);
    
        QuoteApexClass qac = new QuoteApexClass(sc);        
        qac.saveQuoteAsPDFandEmail();
        qac.attachPDF();
        qac.getShowPrintLink();
        qac.print();
    }
}
 I hope this helps you.