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
arun kumar.ax887arun kumar.ax887 

How to increase the code coverage percentage in TestCase

Hi All,

 

    I have a requirement wherein, I need to create the packages in the Invoice, when the record type of the Items is 'package' in the Invoice_Line_Item, then the required number of packages are to be created.

 

   I am getting the desired output, but when I have written the TestCase, the code coverage is 42%.   But, in order to migrate the code on to Live, we need a minimum code coverage of 75%.  

 

  So, could anyone suggest a solution for the above mentioned problem.  I am posting both the Class and TestCase, do suggest a solution.

 

Apex Class

 

 

public class MyClass
{
    
      public static void createPackage(Invoice__c [] inv)
      {
       
         
       for(Invoice__c i : inv)
       {
           Invoice_Line_Item__c[] a1;
           a1 = [select id,name,Item__r.RecordTypeId  from Invoice_Line_Item__c  where invoice_number__c =:i.id and Item__r.RecordTypeId='01290000000K8Ba'];
           Integer j=a1.size();
           for(Invoice_Line_Item__c a2 : a1)
           {
            
            if((i.Create_Package__c==true)||(i.Status__c=='FULLY PAID'))
            {
                 for(Integer k=1;k<=j;k++)
                 {
                 if(i.Package_Status__c!='Package Created')
                  {
                   
                  Package__c p1 = new Package__c();
                  p1.RecordTypeId='01290000000Xn01';
                  p1.invoice__c = i.id;
                  p1.Invoice_Line_Item__c = a2.id;
                  p1.patient__c= i.Patient__c;
                 
                   insert p1;
                 }
                 }
                 
                  i.Package_Status__c='Package Created';
             }
             
            }  
               
             
        }        
                
      }
    }
   
Test Case
@isTest
private class MyClassTestCase {
static testMethod void createPackage() {
item__c v =[select id,name from item__c limit 1];
// invoice__c i = [select id,name, Patient__c from invoice__c limit 1];
               
invoice__c i = [select id,name,Create_Package__c, Status__c, Patient__c, 
                Package_Status__c from invoice__c limit 1];
Invoice_Line_Item__c il = new Invoice_Line_Item__c ();
il.invoice_number__c = i.id;
il.item__c = v.id;
insert il;
if((i.Create_Package__c==true)||(i.Status__c=='FULLY PAID'))
{
         
   if(i.Package_Status__c!='Package Created')
   {
    Package__c p1 = new Package__c();
    p1.RecordTypeId='01290000000Xn01';
    p1.invoice__c = i.id;
    p1.Invoice_Line_Item__c = il.id;
    p1.patient__c= i.Patient__c;
               
    insert p1;
    
    }
    
   i.Package_Status__c='Package Created';
  }
}
}
           
Do suggest a solution and guide me in the right path.
Thanks and Regards
Arun
           
           
           
           
       
       
      

 

Raumil SetalwadRaumil Setalwad

Hello Friend

The case which you have return is wrong. The correct test case should be as follow:

 

public static testMethod void testCase1(){
	
	List<Invoice__c> inv = [select id,name,Create_Package__c, Status__c, Patient__c, 
                Package_Status__c from invoice__c limit 1];
				
				Test.startTest();
					MyClass.createPackage(inv);
				Test.stopTest()'
}

 

 

Hope this helps

Regards

Raumil Setalwad