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
Mohan SelvamMohan Selvam 

Test class reached 69% need to reach 75%

Hi
Please guide me to reach 75% in my test class

My apex class as follows,
Public Class ProductTriggerHandler{
  public void OnAfterUpdate(Map<Id,Product2> newProductMap,Map<Id,Product2> oldProductMap){ 
            Boolean Checkin = false;
            for(Product2 prod: newProductMap.values())  {
            if(prod.cost__c!= oldProductMap.get(prod.id).cost__c){
                    Checkin = True;
                 }
            }    
                
            if(Checkin == true){  
            UpdateGpPercentage(newProductMap,oldProductMap);
            }
        }
   
      Private void UpdateGpPercentage(Map<Id,Product2> newProductMap,Map<Id,Product2> oldProductMap){
    List<OpportunityLineItem> OppProductList = new List<OpportunityLineItem>([ Select id,opportunityid from OpportunityLineItem where PricebookEntry.Product2.id IN :newProductMap.Keyset() and opportunityid != null]);
    List<Id> OppIds = new List<Id>();
      List<Id> SSOppIds = new List<Id>();
      for(OpportunityLineItem oli:OppProductList ){
              OppIds.add(oli.opportunityid);
      } 
    
      List<Opportunity> OppList = new List<Opportunity>();
      List<Opportunity> OppListNew = new List<Opportunity>();
      OppList  = [Select id, Service_Sheet__c from Opportunity where id In :OppIds];
      if(OppList.size() > 0){
          For(Opportunity OPP : OppList){
              if(OPP.Service_Sheet__c!=NULL){
                   SSOppIds.add(OPP.id);

               }
           }
           
           if(SSOppIds.size() > 0){ 
               Try{
                  Database.executeBatch(new UpdateOpportunityByBatch(SSOppIds));

               } catch(Exception E){System.debug('Errors Msg'+E);}
           }
       } 
         
   }
   
}


My test class as follows

@IsTest(SeeallData=True)
Private class ProductTriggerHandlerTest{
    Static TestMethod Void UpdateGpPercentageTest(){
     Test.StartTest(); 
         Product2 Ins_Prod = New Product2(Name='Test Product', ProductCode='DHI_6677_Test', Cost__c=5);
         Insert Ins_Prod;
     Product2  Prod =[Select Id, Cost__c from Product2 where ID=:Ins_Prod.id];
         Prod.Cost__c=10.00;
         Update Prod;
       Opportunity Opp =[Select id,Service_Sheet__c From Opportunity Limit 1];
         List<Id> OppIds = New List<id>();
         OppIds.add(Opp.id);
         Database.executeBatch(new UpdateOpportunityByBatch(OppIds));
     Test.stopTest();   
     
    }
    
}

Note : Unerlined lines are not covered in my test class.. i got 69% ... please guide me to reach 75%. Lines covered 16/23
KaranrajKaranraj
Try the below test class code, you may have to make some modification in the test class. Please look into the comments in test classes
Make sure to include all manadatory fields and proper data for the test object. Don't have practices of using SeeAllData= true in test classes
 
@isTest(SeeallData=false)
Private class ProductTriggerHandlerTest{
    Static TestMethod Void UpdateGpPercentageTest(){
     Test.StartTest(); 
          
          //Create test account with all mandatory fields
          Account act = new Account();
          act.Name='TestAccount';         
          insert act;

          //Create Opportunity records with all mandatory fields
          Opportunity newOpp = new Opportunity();
          newOpp.Name = 'Test Opp';
          newOpp.AccountId = act.Id
          newOpp.Service_Sheet__c = 'Test Sheet';
          insert newOpp;

         Product2 Ins_Prod = New Product2(Name='Test Product', ProductCode='DHI_6677_Test', Cost__c=5);
         Insert Ins_Prod;

         PriceBookEntry pbEntry = new PriceBookEntry();
         pbEntry.UnitPrice = 300;
         pbEntry.PriceBook2Id = Test.getStandardPricebookId();
         pbEntry.Product2Id = newProd.Id;
         pbEntry.IsActive = true;
         insert pbEntry;
         
         Product2  Prod =[Select Id, Cost__c from Product2 where ID=:Ins_Prod.id];
         Prod.Cost__c=10.00;
         Update Prod;

         Test.stopTest();   
    }
    
}


 
Mohan SelvamMohan Selvam
Hi Karanraj

Thanks for your response.. still i am facing the same issue.. 

regards
S Mohan
KaranrajKaranraj
Can you share the update code here? Make sure that you have included the newly created product id in this line pbEntry.Product2Id = newProd.Id;