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
Guru 91Guru 91 

How to write a test class ?

Can any body please suggest how to write a test class for the below code, I am new to Test Class:-

public class QuotePDFController
   { 
   public QuotePDFController() 
    {

    }
    public String Total;
    public String DiscountTotal;
    public String Quote { get; set; }
    public String quoteId {get; set; } 
    public List<Quote> quoteList { get; set; }
    public List<ProductLineItem__c> productLineItems { get; set; }
    public Map<Id, List<ProductLineItem__c>> quotePLIMap { get; set; }
    Set<Id> quoteIds = new Set<Id>();
    Map<Id, Quote> qtMap = new Map<Id, Quote>();
    PRIVATE STATIC FINAL String ID = 'Id';
    
    public QuotePDFController(ApexPages.StandardController controller)
     {  
     quoteId = ApexPages.currentPage().getParameters().get(ID);
        quoteList = [SELECT Id,
                            Name,
                            CBG_PaymentTerms__c,
                            Opportunity.Name,
                            CBG_AllNetReceiptsMInclOption_No_Cur__c,
                            C4C_Date__c,
                            C4C_Date_of_Inquiry__c,
                            CreatedDate,
                            QuoteNumber,
                            CreatedBy.Name,
                            CreatedBy.MobilePhone,
                            CreatedBy.Fax,
                            CreatedBy.Email,
                            Opportunity.CBG_ValidTo__c,
                            Account.BillingCountry,
                            Account.BillingCity,
                            Account.BillingState,
                            Account.BillingPostalCode, 
                            (Select Name,
                                 CBG_Text__c,
                                 CBG_Title__c,
                                 CBG_SectionOnFrom__c 
                                 from  Quote_Texts__r)
                            FROM Quote where id =:quoteId ];
        quotePLIMap = new Map<Id, List<ProductLineItem__c>>();
        
        if(quoteList.size() > 0)
               {
                 for(Quote qt : quoteList)
                  {
                       quoteIds.add(qt.Id);
                       qtMap.put(qt.Id, qt);
                  }
            productLineItems = [SELECT Id,
                                       Name,
                                       CBG_Option__c,
                                       CBG_Quote_Incoterm__c,
                                       Product__r.Name,
                                       External_note__c,
                                       CBG_UnitofMeassure__c,
                                       CBG_Quantity__c,
                                       CBG_ItemLine__c,
                                       CBG_Incoterms__c,
                                       Item_Value_and_option__c,
                                       CBG_QuoteName__c, 
                                       CBG_Product_and_External_Note__c,
                                       CGB_Quantity_and_Measure__c,
                                       Edge_Types__c,CBG_Price__c,
                                       CBG_AdjustedItemValue__c,
                                       CBG_AdjustedItemPrice__c,
                                       (SELECT Name,
                                               test_quantity__c,
                                               CBG_Incoterms__c,
                                               Feight_Percent_Total__c,
                                               CBG_IncotermLocation__c,
                                               CBG_SpecialFreightDummy_Cur__c,
                                               CBG_SpecialFreightDummy_Per__c,
                                               CBG_ProductLineItem__r.name 
                                               FROM Special_Freight__r)
                                       FROM ProductLineItem__c WHERE CBG_QuoteName__c =:quoteId];
                                       
            system.debug('PLI List is ' + productLineItems);
            
        }
       
      
    } 
    
    
}
RKSalesforceRKSalesforce
Hello,

Please find below code to create test class:
@isTest
public class QuotePDFController {
    public static testMethod void testQuotePDFController()
    {        
        //Insert or Update your records here
    }
}
In place of comment you can insert or update your records. In your case you need to insert  ​Quote , Quote_Texts__c, ProductLineItem__c, Special_Freight__c  records and link those records.

Please mark as best answer if helped.

Regards,
Ramakant