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
sridharbarlasridharbarla 

code coverage while running unittest i am get less percentage

Hai,

 

i written a apex class to generate a manual reference number and this code will execute while before insert and before update

 

but while runnig unit test i am getting code coverage is 63%

 

public class AP04
{   
     public static void addNiceRef(Opportunity[] oppObj)  
     {
            String OppDivisionCode;
            Integer refCountValue = 1000 ;
            for (Opportunity getoppObj1:oppObj)
            {      
              OppDivisionCode = getoppObj1.Division_Code__c ;
             }
              
           if(OppDivisionCode != null)
           {
           //   sObject[] groupedResults =
              for (sObject ar : [select Max(RefNumCount__c) refnum, Division_Code__c from Opportunity where Division_Code__c  = :OppDivisionCode group by Division_Code__c limit 1]) 
              {
               if (String.valueOf(ar.get('refnum')) != null)
               {
                  refCountValue = Integer.valueOf(String.valueOf(ar.get('refnum')));
               }
              
              } 
            for (Opportunity getoppObj2:oppObj)
            { 
               getoppObj2.RefNumCount__c = refCountValue + 1;
               getoppObj2.OLDNICENBR__C = OppDivisionCode + '' + getoppObj2.RefNumCount__c ;
            }
          }

     }
     public static void updateNiceRef(Opportunity[] oppObj)  
     {
             String OpporunityID;            
             String OppDivisionCode;            
             Integer refCountValue = 1000 ; 
             Integer RecordCount = 0; 
             for (Opportunity getoppObj1:oppObj)
            {      
                OppDivisionCode = getoppObj1.Division_Code__c ;
                OpporunityID = getoppObj1.ID;
            }
           if(OppDivisionCode != null)
           {
            for (sObject ar : [select Max(RefNumCount__c) refnum,Division_Code__c from Opportunity where  Division_Code__c =:OppDivisionCode group by Division_Code__c limit 1]) 
             {
                     if (String.valueOf(ar.get('refnum')) != null)
                        {
                         refCountValue = Integer.valueOf(String.valueOf(ar.get('refnum')));
                        }
              }  
           }
            for (Opportunity getoppObj2:oppObj)
            {       
               If(OppDivisionCode == null)
               {
                  getoppObj2.OLDNICENBR__C = null;
                  getoppObj2.RefNumCount__c = 0;
               }
               else
                {
                 getoppObj2.RefNumCount__c = refCountValue + 1;
                 getoppObj2.OLDNICENBR__C =  OppDivisionCode + '' + getoppObj2.RefNumCount__c ;
                }
             }

     }

 

test code for my apex class 

 

@istest
private class Test_AP04_AP05 {


 private static testMethod void TestAP04_5(){
 test.starttest();
    List<Opportunity> myOpportunityList1=new List<Opportunity>(); 
     Account a = new account();
    a.Name = 'Test';
    a.RecordTypeId = '012200000001RhOAAU';
    a.KeyAccount__c = 'No';
    insert a;
   for(Integer i=0;i<5;i++)
   { 
         myOpportunityList1.add(new Opportunity(RecordTypeId = '012200000001RetAAE',name='Testing'+ i ,
         Amount=100000,StageName='Prospecting',
         CloseDate=date.today(),AccountId=a.Id,Division_Code__c = 'UKTY2',Market_Community__c ='Latin_America',
         Trading_Plan__c='No' ));   
         
         myOpportunityList1.add(new Opportunity(RecordTypeId = '012200000001RetAAE',name='Testing'+ i ,
         Amount=1000,StageName='Prospecting',
         CloseDate=date.today(),AccountId=a.Id,Division_Code__c = 'UKTY2',Market_Community__c ='Other',
         Trading_Plan__c='Yes' )); 
             
         myOpportunityList1.add(new Opportunity(RecordTypeId = '012200000001RetAAE',name='Testing'+ i ,
         Amount=1000,StageName='Prospecting',
         CloseDate=date.today(),AccountId=a.Id,Division_Code__c = '',Market_Community__c ='',
         Trading_Plan__c='' ));  
            
         myOpportunityList1.add(new Opportunity(RecordTypeId = '012200000001RetAAE',name='Testing'+ i ,
         Amount=1000100,StageName='Prospecting',
         CloseDate=date.today(),AccountId=a.Id,Division_Code__c = '',Market_Community__c ='Latin_America',
         Trading_Plan__c='No' ));     
    }     
     insert myOpportunityList1;  
     test.stoptest();       
   }   
  
}

 

 

if i am increaing for lopp size it is showing tomany soql quaries

please any body help me on this

wchristnywchristny

Your test method needs to account for both branches of your "if...else" clauses and ensure that the queries in each of your "for" loops return at least one row.

 

When you run the test, the 63% coverage figure should be underlined.  Click that link and you'll get a pop-up window which tells you exactly how many times each line of your code was executed.  Focus on the lines where the count is zero and adjust your test method to force the execution of those lines.  For example, in an "if...else" clause, you may need one record to satisfy the "true" condition and another record to satisfy the "false" condition.