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%

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({
                    Checkin = True;
            if(Checkin == true){  
      Private void UpdateGpPercentage(Map<Id,Product2> newProductMap,Map<Id,Product2> oldProductMap){
    List<OpportunityLineItem> OppProductList = new List<OpportunityLineItem>([ Select id,opportunityid from OpportunityLineItem where IN :newProductMap.Keyset() and opportunityid != null]);
    List<Id> OppIds = new List<Id>();
      List<Id> SSOppIds = new List<Id>();
      for(OpportunityLineItem oli:OppProductList ){
      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(SSOppIds.size() > 0){ 
                  Database.executeBatch(new UpdateOpportunityByBatch(SSOppIds));

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

My test class as follows

Private class ProductTriggerHandlerTest{
    Static TestMethod Void UpdateGpPercentageTest(){
         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];
         Update Prod;
       Opportunity Opp =[Select id,Service_Sheet__c From Opportunity Limit 1];
         List<Id> OppIds = New List<id>();
         Database.executeBatch(new UpdateOpportunityByBatch(OppIds));

Note : Unerlined lines are not covered in my test class.. i got 69% ... please guide me to reach 75%. Lines covered 16/23
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
Private class ProductTriggerHandlerTest{
    Static TestMethod Void UpdateGpPercentageTest(){
          //Create test account with all mandatory fields
          Account act = new Account();
          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];
         Update Prod;


Mohan SelvamMohan Selvam
Hi Karanraj

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

S Mohan
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;