+ Start a Discussion
DevmenDevmen 

code coverage for trigger in for loop

Hi ,

Here is the trigger  im getting 57% code coverage.Commented area in trigger is not getting covered ,Below is the test class.
For(Opportunity svp:trigger.new) 
        {
            if(svp.Generate_Payment_Schedule__c== true) 
            {
            /*   for(Payment_Schedule_Template__c P:PSTList) 
               
                {
                  system.debug('*************************PS'+PSTList);
                taxlist = [select id,Name from Tax_Master__c WHERE Name='GST' OR  Name= 'Vat & Stax'];
                   system.debug('TTTTTTTTTTTTTTTTAXLIST'+taxlist);
                    for(Tax_Master__c tm:taxlist)
                    {
                    taxid=tm.id;
                    taxName=tm.Name;
                  }
                   system.debug('TTTTTTTTTTTTTTTTAXID'+taxid);
                   // Prec+=P.Percentage__c; 
                   if(taxName=='GST')
                   { 
                PSList.add(new Schedule__c(Display_Order__c = P.Display_Order__c,
                                           Name = P.Name,
                                          Cumulative_Percentage__c=p.Cumulative_Percentage__c,
                                           Cumulative_Installment_Amt__c=p.Cumulative_Percentage__c/100*svp.Total_Agreement_Value__c,
                                           Percentage__c = P.Percentage__c,
                                           Tentative_Due_Date__c=p.Tentative_Due_Date__c,
                                           Installment_Amount__c=p.Percentage__c/100*svp.Total_Agreement_Value__c,                                               
                                           Opportunity__c = myatd[1],
                                           Tax_Master__c = taxid
                 
                                           )); 
                     }
                     else if(taxName=='Vat & Stax')
                     {
                      PSList.add(new Schedule__c(Display_Order__c = P.Display_Order__c,
                                           Name = P.Name,
                                           Cumulative_Percentage__c=p.Cumulative_Percentage__c,
                                           Cumulative_Installment_Amt__c=p.Cumulative_Percentage__c/100*svp.Agreement_Value__c,
                                           Percentage__c = P.Percentage__c,
                                           Tentative_Due_Date__c=p.Tentative_Due_Date__c,
                                           Installment_Amount__c=p.Percentage__c/100*svp.Agreement_Value__c,                                               
                                           Opportunity__c = myatd[1],
                                          // Cumulative_Percentage__c=Prec,
                                           Tax_Master__c = taxid
                 
                                           )); */
                     
                     }    
                                        
             
                      
                  
                   }                                           
                }
                 
                  
            }
           
        
           
         insert PSList;
        PSList.clear();
        PSTList.clear();
        ut.clear();
       myatd.clear();  
       } 
    }
 
static testMethod void ScheduleTest() 
    {
        Tax_Master__c tm = new Tax_Master__c(Name='GST');
        insert tm;
        Account a = new Account(name='Ravi');
        insert a;
        
        
        Project__c pr = new Project__c(Name='Laplazzo');
        insert pr;
        Block__c b = new Block__c(Name='block one',Project_Name__c=pr.id);
        insert b;  
         Apartment__c ap = new Apartment__c(Name='112', Block__c=b.Id, Status__c='Available');
        insert ap;
        
        List<Payment_Schedule_Template__c> PSTList = new List<Payment_Schedule_Template__c>();
        for (integer i = 1; i <10; i++)
        {
        PSTList.add(new Payment_Schedule_Template__c(
                    Name = 'Payment schedule One', Percentage__c = 0.00, Block__c = b.id,
                    Tentative_Due_Date__c = System.Today(),Display_Order__c = i));            
        }
        Insert PSTList;
       
        Opportunity o=new Opportunity(Name='raviopp',AccountId=a.Id,StageName='Blocking', Apartment__c =ap.id,CloseDate=Date.today(), Generate_Payment_Schedule__c = True);
        insert o;   
        system.debug('OPPPPPPPPPPPP'+o);
        if(o.Generate_Payment_Schedule__c)
        {
        system.debug('OOOOOOOOOOOOOOOOO'+o.Generate_Payment_Schedule__c);
            List<Payment_Schedule_Template__c> PSList = new List<Payment_Schedule_Template__c>();
            for (Payment_Schedule_Template__c P: PSTList)
            {
            system.debug('PPPPPPPPPPSTLIST'+PSTList);
            Tax_Master__c tm1 = new Tax_Master__c(Name='GST');
             insert tm1;
             Schedule__c sch= new Schedule__c(Display_Order__c=1,Name='sch1',Cumulative_Percentage__c=0.06,Tentative_Due_Date__c=System.Today(),Opportunity__c=o.id);
             insert sch;
            PSList.add(new Payment_Schedule_Template__c(
                      Name = P.Name,Block__c=b.id,Percentage__c = P.Percentage__c,
                      Tentative_Due_Date__c = System.Today(), Display_Order__c = P.Display_Order__c));
            }
            Insert PSList;
            update PSList;
        }
         update o;
   }    
}
Pls help me to resolve this .
 
Paul S.Paul S.
To obtain code coverage within an if statement, you need to test both positive and negative scenarios.  Create a second test method where Generate_Payment_Schedule__c = false and you should see that block covered.