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
kaushik Sabapathykaushik Sabapathy 

test class for isUpdate and trigger.Oldmap

Hi all,
try
        {
           for(OpportunityLineItem opp:oli1) 
                
         {
          OpportunityLineItem Oldopp=opp;
         
           if(Trigger.isUpdate)                  
       
         {
        
         oldOpp = (OpportunityLineItem ) Trigger.oldMap.get(opp.Id);     
         
         }                                       
         if(filterStages.contains(opp.Product_Type__c)&&filterStages.contains(opp.TAV_Category__c)&&filterStages.contains(opp.Interval__c)&&filterStages.contains(opp.Line_Transaction_Type__c)){
         if(opp.Term__c<=12)
         {
         opp.TAV__c=(opp.Monthly_Net_Price__c-oldOpp.Monthly_Net_Price__c)*opp.Term__c;
if(opp.TAV__c<0)
opp.TAV__c=0;
}
         else
         {
         if(opp.Term__c<=12)
         {
         opp.TAV__c=(opp.Monthly_Net_Price__c-oldOpp.Monthly_Net_Price__c)*12;
         if(opp.TAV__c<0)
opp.TAV__c=0;
         }
          }
                   }

The test class covers 77% of the code but  im not able to cover the apex class for the above part. Below is the entire piece of code
Public class c15
{
Static Boolean isCalculated=false;
Public Static void tavcal(OpportunityLineItem[] oli)
{
if(isCalculated==false){
//String result;SELECT Id, Name, (SELECT Id, Name FROM Relationship_Name__r) FROM Parent_Object_API_Name__c
//List<Opportunity> direct2 = [Select Id,Type,(Select Id, Product_Type__c,Line_Transaction_Type__c,Monthly_Net_Price__c,TAV_Category__c,TAV__c,Term__c,Interval__c FROM OpportunityLineItems) From Opportunity];
  Map < String, OpportunityLine__c> customSettingsMap =OpportunityLine__c.getAll();
           String filterStages = customSettingsMap.get('New value').Product_Type__c;
           String secondStages=customSettingsMap.get('second value').Product_Type__c;
           String thirdStages=customSettingsMap.get('third value').Product_Type__c;
          // String fourthStages=customSettingsMap.get('initial criteria').Product_Type__c;
         //  String initialvalue=customSettingsMap.get('initial criteria').Product_Type__c;
//           CustomSettingName__c mc = CustomSettingName__c.getValues(data_set_name);

       System.debug('Filtered Stages == '  + filterStages);
       System.debug('Filtered Stages == '  +secondStages );
       
         

       
       //if(allParentRecords.size() > 0 && allParentRecords.containsKey(record.OpportunityId))
      
        try
        {
        if(Trigger.isUpdate||Trigger.isInsert)
        
        {
         for(OpportunityLineItem opp1:oli)
         {
        // OpportunityLineItem oldOpp = (OpportunityLineItem)Trigger.oldMap.get(opp1.Id);
          
                                       
         if(filterStages.contains(opp1.Product_Type__c)&&filterStages.contains(opp1.TAV_Category__c)&&filterStages.contains(opp1.Interval__c)&&(opp1.Line_Transaction_Type__c==null)){
         if(opp1.Term__c<=12)
         opp1.TAV__c=opp1.Monthly_Net_Price__c*opp1.Term__c;
         else
         opp1.TAV__c=opp1.Monthly_Net_Price__c*12;
                       }
         else  if(secondStages.contains(opp1.Product_Type__c)&&secondStages.contains(opp1.TAV_Category__c)&&(opp1.Interval__c==null))
         {
        
         opp1.TAV__c=opp1.Monthly_Net_Price__c;
         }
         else 
         {
         if(thirdStages.contains(opp1.Product_Type__c)&&thirdStages.contains(opp1.TAV_Category__c)&&thirdStages.contains(opp1.Interval__c)&&opp1.Line_Transaction_Type__c==null){
         if(opp1.Term__c>12)
          opp1.TAV__c=((opp1.Monthly_Net_Price__c *36) / 0.48) * 0.2;
         else 
          opp1.TAV__c=opp1.Monthly_Net_Price__c;
          }
         }
                      
         
         }
         }
              }
         catch (System.NullPointerException e) {

}


         isCalculated = true;
    //  update opp;
       //  listof.add(opp);
        
        
        
        
        
           
       //  update listof; 
         
          
}/* end of for loop*/            
} /*end of if*/    
Public Static void tavcal1(OpportunityLineItem[] oli1)
{
if(isCalculated==false){
//String result;
//List<OpportunityLineItem> direct2=[SELECT Product_Type__c,Line_Transaction_Type__c,Monthly_Net_Price__c,TAV_Category__c,TAV__c,Term__c,Interval__c FROM OpportunityLineItem where Opportunity.Type='Renewal'];
  Map < String, OpportunityLine__c> customSettingsMap =OpportunityLine__c.getAll();
           String filterStages = customSettingsMap.get('Renewal').Product_Type__c;
           String secondStages=customSettingsMap.get('second value').Product_Type__c;
           String thirdStages=customSettingsMap.get('third value').Product_Type__c;
//           CustomSettingName__c mc = CustomSettingName__c.getValues(data_set_name);

       System.debug('Filtered Stages == '  + filterStages);
       System.debug('Filtered Stages == '  +secondStages );
      

       
       //if(allParentRecords.size() > 0 && allParentRecords.containsKey(record.OpportunityId))
       
         
        
         
        try
        {
           for(OpportunityLineItem opp:oli1) 
                
         {
          OpportunityLineItem Oldopp=opp;
         
           if(Trigger.isUpdate)                  
       
         {
        
         oldOpp = (OpportunityLineItem ) Trigger.oldMap.get(opp.Id);     
         }                                       
         if(filterStages.contains(opp.Product_Type__c)&&filterStages.contains(opp.TAV_Category__c)&&filterStages.contains(opp.Interval__c)&&filterStages.contains(opp.Line_Transaction_Type__c)){
         if(opp.Term__c<=12)
         {
         opp.TAV__c=(opp.Monthly_Net_Price__c-oldOpp.Monthly_Net_Price__c)*opp.Term__c;
if(opp.TAV__c<0)
opp.TAV__c=0;
}
         else
         {
         if(opp.Term__c<=12)
         {
         opp.TAV__c=(opp.Monthly_Net_Price__c-oldOpp.Monthly_Net_Price__c)*12;
         if(opp.TAV__c<0)
opp.TAV__c=0;
         }
          }
                   }
                   
                   if(trigger.isInsert||trigger.isUpdate)
                   {
          if(secondStages.contains(opp.Product_Type__c)&&secondStages.contains(opp.TAV_Category__c)&&opp.Interval__c==null)
         {
        
         opp.TAV__c=opp.Monthly_Net_Price__c;
         }
         else 
         {
         if(thirdStages.contains(opp.Product_Type__c)&&thirdStages.contains(opp.TAV_Category__c)&&thirdStages.contains(opp.Interval__c)&&thirdStages.contains(opp.Line_Transaction_Type__c)){
         if(opp.Term__c>=12)
          opp.TAV__c=((opp.Monthly_Net_Price__c *36) / 0.48) * 0.2;
         else 
          opp.TAV__c=opp.Monthly_Net_Price__c;
          }
         }
                      
         }
         }
         
         }
         
         catch (System.NullPointerException e) {

}
         isCalculated = true;
       //  update opp;
           
         
         
         
}
}  

}

 This is the test method I assumed that it may work and modified it several times :-) for the code part which needs to be covered.
 
static testMethod void tavcalTest7() {
       Product2 newProd = new Product2(Name = 'test product', family = 'test family');
insert newProd;
Id pricebookId = Test.getStandardPricebookId();

PriceBookEntry pbEntry = new PriceBookEntry(
    UnitPrice = 300,
    PriceBook2Id = pricebookId,
    Product2Id = newProd.Id,
    IsActive = true);

insert pbEntry ;
Opportunity oppObj = new Opportunity(Name = 'TestOpp',Amount = 2000.00,CloseDate=Date.today(),StageName='Close Won',Type='Renewal');
insert oppObj;

insertcustomsetting();
//List<OpportunityLineItem> listopp=new List<OpportunityLineItem>();

OpportunityLineItem oppLine = new OpportunityLineItem(pricebookentryid=pbEntry.Id,TotalPrice=2000.00,Quantity = 2.00,OpportunityID = oppObj.Id,
Product_Type__c='Subscription',
TAV_Category__c='MRR',
Interval__c ='Monthly',
Monthly_Net_Price__c=300,
Term__c=2,
Line_Transaction_Type__c='No-change');
insert oppLine;



           // oppLine = [SELECT Product_Type__c,TAV_Category__c,Interval__c ,Monthly_Net_Price__c,Term__c,TAV__c FROM OpportunityLineItem WHERE Id =:oppLine.Id];
 //system.assertEquals(checkString, [SELECT Description FROM Account WHERE Id = :acct.Id].Description);
   // system.assertEquals(checkString, [SELECT Description FROM Contact WHERE Id = :c.Id].Description);
Test.starttest();
oppLine = [SELECT Product_Type__c,TAV_Category__c,Interval__c ,Monthly_Net_Price__c,Term__c,TAV__c FROM OpportunityLineItem WHERE Id =:oppLine.Id];
oppLine.Monthly_Net_Price__c=200;
update oppLine;
System.assertEquals(200,[SELECT TAV__c FROM OpportunityLineItem WHERE Id =:oppLine.Id].TAV__c);
Test.stoptest();
}

Please guide me to achieve a better Solution
Thaks,
kaushik