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
Shruthi NarsiShruthi Narsi 

Test Class for Create PDF

I have written a test class to create PDF on quote. Can anyone help me with the test class and make the code covergae as 90% atleast. Since the logic of ny test class is wrong

Apex class
public class QuotePDF {
    public boolean show{get;set;}
    public id pdtId{get;set;}
    public Quote op{get;set;}
    public QuoteLineItem oli{get;set;}
    public string pb{get;set;}
    public Id opid{get;set;}
    public id prodid{get;set;}
    public list<QuoteLineItem> OliList{get;set;}
    public list<Quotelineitem> OliCreate;
    public integer deleteIndex{get;set;}
    
    public QuotePDF()
    {
        show=false;
        oli=new QuoteLineItem();
        op=new Quote();
        OliList=new list<QuoteLineItem>();
        OliCreate=new list<Quotelineitem>();
    }
    
    public void save(){
        pricebook2 spbid=[select id from pricebook2 where isStandard=true limit 1];
        
        opportunity o1=[select id from opportunity limit 1];
        op.OpportunityId=o1.id;
        op.Pricebook2Id=spbid.id;
        upsert op;
        map<id,id>pdtTopbemap=new map<id,id>();
        
        for(pricebookentry pb:[select product2id,id from pricebookentry where pricebook2id =:spbid.id ])
        {
            pdtTopbemap.put(pb.product2id,pb.id);
        }
        for(QuoteLineItem o:OliList)
        {
            o.QuoteId=op.id;
            o.unitprice=o.unitprice; 
            o.quantity=o.quantity;
            o.pricebookentryid=pdtTopbemap.get(o.product2id);
            o.Product2Id=o.product2id;
            OliCreate.add(o);
        }
        insert OliCreate;
    }
    public void AddOli()
    {
        show=true;
        QuoteLineItem olitemp = new QuoteLineItem();
        OliList.add(olitemp);
    }
    
    public void deleteOpp()
    {
        OliList.remove(deleteIndex);
    }
}

VF

<apex:page standardController="Quotes__c" renderAs="{!renderAs}" extensions="QuoteApexClass" sidebar="false">
    <apex:form id="form1" rendered="{!showPrintLink}" style="width:700px; margin:auto; float:center;font-family:Times New Roman;font-size: 18px;"> 
        <Right>
            <apex:commandButton action="{!print}" value="Save to PDF" style="float:center;color: #0A3FF3; font-size: 17px;right:0px;position:relative;border-radius:1px;"/>
            <apex:commandButton action="{!saveQuoteAsPDFandEmail}" value="Save and Email" style="float:center;color: #0A3FF3; font-size: 17px;right:-20px;position:relative; border-radius:1px;"/>
            <apex:commandButton action="{!cancel}" value="Cancel" style="float:center;color: #0A3FF3; font-size: 17px;right:-40px;position:relative; border-radius:1px;"/>
        </Right>
        <br/><br/><br/>
        <apex:outputPanel rendered="{!If(selectedList=='Standard' , true, false)}">
            <html>
                <head>
                    <div style="background-color: #ffe4c4;margin-top: -32px;">
                        <apex:image url="{!URLFOR($Resource.Proseralogo)}" width="200" height="60" alt="plogo"/><br/><br/>
                        <b style="font-size: 20px;">{!$Organization.Name}</b>
                    </div>
                </head>
                <body>
                    <br/> 
                    <table width="100%"> 
                        <tr><td style="background-color:#add8e6;" colspan="6">General Information</td></tr>
                        <tr>
                            <td width="66%">Company Address: {!$Organization.Country}</td>
                            <td></td>
                            <td>Quote Number:{!Quotes__c.Quote_Number__c}</td>
                        </tr>
                        <tr>
                            <td></td>
                            <td></td>
                            <td>Expiration Date:<apex:outputField value="{!Quotes__c.Expiration_Date__c}"/></td>
                        </tr>
                    </table><br/>
                    <div style="background-color:#add8e6;">Contact Information </div>
                    <table>
                        <tr>
                            <td>Prepared By:</td>
                        </tr>
                        <tr>
                            <td>Email:{!Quotes__c.Email__c}</td>
                        </tr>
                    </table><br/>
                    <div style="background-color:#add8e6;">Address Information</div>
                    <table>
                        <tr>
                            <td>
                                Bil To:<p style="margin-left: 49px;margin-top: -20px;margin-right: 311px;font-size: 15px;">
                                {!Quotes__c.Bill_To__c}
                                </p>
                            </td>
                        </tr>
                        <tr>
                            <td style="font-size: 15px;">Ship To:{!Quotes__c.Quote_Number__c}</td>
                        </tr>
                    </table><br/>
                    <div style="background-color:#add8e6;">Product Details</div>
                    <table style=" width:100%;">
                        <tr border="1" style="background-color: #deb887;"> 
                            <th style="text-align:center">Product Name</th>
                            <th style="text-align:center">List Price</th>
                            <th style="text-align:center">Sales Price</th>
                            <th style="text-align:center">Quantity</th>
                            <th style="text-align:center">Total Price</th>  
                        </tr>
                        <apex:repeat value="{!Quotes__c.Quote_Line_Items__r}" var="QuoteLine">
                            <tr style="border: 1px solid #ddd; background-color: #f2f2f2;background-color: #ddd;">
                                <td style="text-align:center">{!QuoteLine.Product2Id__r.Name}</td>
                                <td style="text-align:center">{!QuoteLine.ListPrice__c}</td>
                                <td style="text-align:center">{!QuoteLine.UnitPrice__c}</td>
                                <td style="text-align:center">{!QuoteLine.Quantity__c}</td>
                                <td style="text-align:center">{!QuoteLine.Total_Price__c}</td>
                            </tr>
                        </apex:repeat>
                    </table><br/>
                    <div style="background-color:#add8e6;">Totals</div>
                    <table>
                        <tr>
                            <td>Sub Total:₹<apex:outputLabel escape="false" value="{!Quotes__c.SubTotal__c}"/></td>
                        </tr>
                        <tr>
                            <td>Discount:₹<apex:outputLabel escape="false" value="{!Quotes__c.Discount__c}"/></td>
                        </tr>
                        <tr>
                            <td>Total Price:₹<apex:outputLabel escape="false" value="{!Quotes__c.Total_Price__c}"/></td>
                        </tr>
                        <tr>
                            <td>Tax:₹<apex:outputLabel escape="false" value="{!Quotes__c.Tax__c}"/></td>
                        </tr>
                        <tr>
                            <td>Shipping and Handling:₹<apex:outputLabel escape="false" value="{!Quotes__c.ShippingHandling__c}"/></td>
                        </tr>
                        <tr>
                            <td>Grand Total:₹<apex:outputLabel escape="false" value="{!Quotes__c.Grand_Total__c}"/></td>
                        </tr>
                    </table><br/>
                    <div style="background-color:#add8e6;margin-bottom: 6px;">Quote Acceptance Information</div>
                    <table>
                        <tr>
                            <td style="margin-bottom: 6px;">Signature:........................</td>
                        </tr>
                        <tr>
                            <td style="margin-bottom: 6px;">Name:..............................</td>
                        </tr>
                        <tr>
                            <td style="margin-bottom: 6px;">Title:..............................</td>
                        </tr>
                        <tr>
                            <td style="margin-bottom: 6px;">Date:...............................</td>
                        </tr>
                    </table>
                </body>
            </html>

Test Class

@isTest
Public class QuoteApexClassTest {
    @isTest public static void QuoteApextestMethod() {
 Opportunities__c opp = new Opportunities__c();
        opp.Name = 'TestOpp';
        opp.Close_Date__c = System.today() + 5;
        opp.Stage__c = 'Prospecting';
        Insert opp;
        
        Quotes__c q = new Quotes__c();
        q.Name = 'test';
        q.OpportunityId__c = 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);

    
    PageReference pref = Page.QuoteTemplate;
      
    pref.getParameters().put('id',opp.id);
    Test.setCurrentPage(pref);

   


  }
}
AbhishekAbhishek (Salesforce Developers) 
Hi Shruthi,

Try the code as mentioned in the below blog,

https://salesforce.stackexchange.com/questions/34294/test-class-for-controller-of-visualforce-page-that-renders-as-a-pdf

It might help you.

Thanks.