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
St57 MSt57 M 

Test Class for Opportunity

My test class is not working:

Apexclass:

public with sharing class OpportunityPDF {

    public Opportunity opp{get;set;}
    public list<OpportunityLineItem> product{get;set;}
    public list<OpportunityLineItem> product1{get;set;}
    public Double total{get;set;}
    public Decimal discount{get;set;}
    public Decimal grandTotal{get;set;}
    public Date Today { get { return Date.today(); }}
    public Date lastDate{get;set;}
    public List<terms_and_conditions__c> termsList{get;set;}
    public List<terms_and_conditions__c> termsList12{get;set;}
    public List<WrapperClassEx> wrapperObj{get;set;}
    public Boolean checkBox{get;set;}
    public Boolean displayPopup {get;set;}
    public set<string> TCNames {get;set;}
    public list<Terms_and_Conditions__c> TRC{get;set;}
  
    public OpportunityPDF(){
            opp = [select id,CreatedBy.Email,Opportunity_Number__c,CreatedBy.Fax,CreatedBy.Phone,Account.Name, Account.BillingCity , Account.BillingCountry , Account.BillingState ,Account.BillingStreet,Account.BillingPostalCode ,Account.Insurance_Company__c ,
                      Contract_Start_Date__c,Contract_Term_Months__c,CloseDate,Payment_Terms__c,TotalOpportunityQuantity,Name,Shipping_Method__c,Shipping_Terms__c    from Opportunity where id = :Apexpages.currentpage().getParameters().get('id')];
       
            product = [select id,Opportunity.Id ,Total__c,Discount,Billing_Frequency__c,Billing_Type__c,UnitPrice,Quantity ,Description,TotalPrice,PricebookEntry.Product2.Name  from OpportunityLineItem where Opportunity.Id  = :opp.id and Billing_Type__c = 'Initial'];
       
            product1 = [select id,Opportunity.Id ,Billing_Frequency__c,Total__c,Billing_Type__c,UnitPrice,Quantity ,Description,TotalPrice,PricebookEntry.Product2.Name  from OpportunityLineItem where Opportunity.Id  = :opp.id and Billing_Type__c = 'Subscription'];
            total = 0.00;
            discount = 0.00; 
            grandTotal = 0.00;
            for(integer i = 0;i<product.size();i++){
                /*Calculate sum of total*/
                total+= product[i].Total__c;
                if(product[i].discount!=Null){
                    discount+= product[i].Total__c-product[i].TotalPrice;   
                }
                    grandTotal= total-discount;
            }
            Date todayDate = System.today();
            Date firstDate = todayDate.toStartOfMonth();
            lastDate = firstDate.addDays(date.daysInMonth(todayDate.year(), todayDate.month()) -1 );
      
            checkBox = false;
            wrapperObj = new List<WrapperClassEx>();
            TRC = new  list<Terms_and_Conditions__c>();
            String decodedUrl = EncodingUtil.urlDecode(Apexpages.currentpage().getParameters().get('TCList'), 'UTF-8');
            set<string> TCNames = new set<string>();
            List<String> TcNamesArray = decodedUrl.split(',,',0);
            for(integer i=0;i<TcNamesArray.size();i++){
                TCNames.add(TcNamesArray[i]);
            }
            termsList = [Select id,name,subject__c,Description__c from Terms_and_Conditions__c where name in :TCNames ];
            for(Terms_and_Conditions__c ter: termsList){
                String termDesc = ter.Description__c;
                termDesc = termDesc.replace('Contract Term',+String.valueOf(opp.Contract_Term_Months__c));
                Datetime myDatetime = (opp.Contract_Start_Date__c).addDays(1);
                String myDatetimeStr = myDatetime.format('MMMM d,  yyyy');
                termDesc = termDesc.replace('Start Date',myDatetimeStr);
                termDesc = termDesc.replace('payment term',+String.valueOf(opp.Payment_Terms__c));
                ter.Description__c = termDesc;
                wrapperObj.add(New WrapperClassEx(ter,checkBox ));
            }       
    }
  
   public Class WrapperClassEx{
   public Terms_and_Conditions__c termObj{get;set;}
   public Boolean checkBox{get;set;}
  
   public WrapperClassEx(Terms_and_Conditions__c termRec,boolean checkBox ){
        this.termObj = termRec;
        this.checkBox = checkBox ;
        if(test.isrunningtest()){
               this.checkBox = true;
            }
     }
   }
}

Test Class:code coverage is 54%....

@isTest
public class OpportunityPDF_TC
{
    static testmethod void method1()
    {
        test.startTest(); 
        CloneOpportunityTrigger__c clone = new CloneOpportunityTrigger__c();
        clone.Trigger_Status__c = true;
        clone.setupownerid = UserInfo.getUserId();
        insert clone;
       
        Terms_and_Conditions__c term = new Terms_and_Conditions__c();
        Boolean bool=true;
        term.Name = 'GPS';
        term.Subject__c = 'GPS';
        term.Description__c = 'des';
        insert term;
       
        Opportunity opp1 = new Opportunity(name='Test Opp 1',StageName = 'Closed Won',CloseDate = Date.newInstance(2009,01,01), Pricebook2Id='01s800000003VCr',
                                                 Renewable__c = true,Contract_Start_Date__c = Date.newInstance(2015,01,01),Contract_Term_Months__c = 1.0);
          
        insert opp1;
       
      

        system.assertEquals(opp1.name,'Test Opp 1');
        ApexPages.CurrentPage().getParameters().put('id',opp1.id);
        String encoded = EncodingUtil.urlEncode('test', 'UTF-8');
        String TRC = Apexpages.currentpage().getParameters().put('test',opp1.name);
        String decodedUrl = EncodingUtil.urlDecode('TRC','UTF-8');
        OpportunityPDF pdf = new  OpportunityPDF();
   
       
        pdf.checkBox = true;
        test.stopTest();
       
    }
  
}

i am getting an error like:

Error Message System.NullPointerException: Argument cannot be null.
Stack Trace (System Code)
Class.OpportunityPDF.<init>: line 50, column 1
Class.OpportunityPDF_TC.method1: line 37, column 1

Could you please help me any one,it is very urgent requiremnt 
Phillip SouthernPhillip Southern
Hi, can you confirm which statement is line 50?  It's hard to tell, but I'm thinking is where you perform SOQL for termsList...and you do that before instantiating, so if there are no results that could produce the error.

So before you perform the SOQL or in the controller do: termsList = new List<terms_and_conditions__c>();