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 write a Test Case for the following Application

Hi,

 

I have written an Apex class and a Trigger associated with it, to get the user defined Auto Number in string format.  I have got the desired output, but when I am writing the Test Cases, I am getting the Code Coverage as 0%.  In order to place the application to the production mode we are suppose to get a minimum of  75 % code coverage.  I am posting the Trigger,  Apex class and the Test Case.   So could any one please suggest a solution for the this problem so that code coverage percentage is greater than 75 %.

 

 

Trigger


 

trigger DRTrigger on DR__c (before insert) 
{
    DR__c [] dr =  Trigger.new;
    DRNumber.getDRNum(dr);
}

 


 

 

 

Apex Class

 

 

public class DRNumber
{
    public static String ToString(Decimal Value)
     {
         
            return Value.format();
     } 

     
    public static void getDRNum(DR__c [] dr)
    {
       Integer m;
       
       
       for(DR__c i : dr)
       {
                        
                Branch__c b =[select id, Name, Branch_Code__c, DRAutoNumber__c  from Branch__c              
                                                                                                                             where id =:i.Branch__c];          
                   
                m=b.DRAutoNumber__c.intValue();                            
                m=m+1;
                i.num1__c=m;
                b.DRAutoNumber__c=m;
                String s1 = ToString(i.num1__c);
    
                                         
                                                       
               i.DRNumber__c = 'DR'+'/'+b.Branch_Code__c+'-'+s1;
               i.Name =  i.DRNumber__c;
               update b; 
               }
        }
}

 

 

Test Case


@isTest
private class DRNumberTestCase
{
    public static String ToString(Decimal Value)
     {
         
            return Value.format();
     } 

     
    static testMethod void getDRNum()
    {
       Integer m;
       
      List<DR__c> dr = new List<DR__c>();
       for(DR__c i : dr)
       {
                        
                Test.startTest();          
                Branch__c b = [select id, Name, Branch_Code__c, DRAutoNumber__c  from Branch__c              
                                                                   where id =:i.Branch__c];                            
                m=b.DRAutoNumber__c.intValue();                            
                m=m+1;
                System.assertEquals(i.num1__c, m);
                System.assertEquals(b.DRAutoNumber__c, m);
                String s1 = ToString(i.num1__c);
     
                                                   
                i.DRNumber__c = 'DR'+'/'+b.Branch_Code__c+'-'+s1;
                System.assertEquals(i.Name, i.DRNumber__c);
                update b; 
              Test.stopTest();
                  
               }
        }
}



 


 


sfdcfoxsfdcfox

Your test defines an empty list, then attempts to iterate through the empty list. As a result, the code does nothing and performs no coverage of your trigger.

 

Your "class" is also incorrect; do not place queries inside a loop. It is considered "not bulk safe."

 

Try this out:

 

Trigger

 

 

trigger DRTrigger on DR__c (before insert) {
  DRNumber.autoNumber(Trigger.new);
}

Class

 

public class DRNumber {
  public static void autoNumber(DR__c[] drList) {
    // Which branches are mentioned in this trigger?
    Map<Id,Branch__c> branches;
    {
      // Build a
      Set<Id> branchIds = new Set<Id>();
      for(DR__c dr:drList)
        branchIds.add(dr.branch__c);
      branches = new Map<Id,Branch__c>([select id,name,branch_code__c,drautonumber__c from branch__c where id in :branchIds for update]);
    }
    for(DR__c dr:drList)
      if(branches.containsKey(dr.branch__c))
        dr.drnumber__c = 'DR/' +
          branches.get(dr.Branch__c).Branch_Code__c + '-' +
          String.valueOf((++branches.get(dr.Branch__c).DRAutoNumber__c));
    update branches.values();
}

Test

 

 

 

@isTest
public class DRNumberTest {
  public static testMethod void testNumber() {
    // Create a couple new branches
    List<Branch__c> newBranches = new List<Branch__c>{
      new Branch__c(name='East',branch_code__c='EST',drautonumber__c=10),
      new Branch__c(name='West',branch_code__c='WST',drautonumber__c=15) };
    insert newBranches;

    // Create a map of these for convenience.
    Map<Id,Branch__c> branches = new Map<Id,Branch__c>([select id,name,branch_code__c,drautonumber__c from branch__c where id in :newBranches]);

    // Make some new DR records.
    List<DR__c> newDrList = new List<Dr__c>();
    // Populate some sample data.
    for(Branch__c theBranch:branches.values())
      newDrList.add(new DR__c(Branch__c=theBranch.Id)); // add whatever other data you need here to pass validation
    // Trip the trigger.
    insert newDrList;
    // Query to see what the results are...
    newDrList = [select id,name,branch__c from DR__c where id in :newDrList];
    // Check and make sure they're okay. Format here should be identical to the class function...
    // ++Variable is the prefix type autoincrement, meaning the value will get adjusted before assignment, same as your old code.
    for(DR__c DR:newDrList)
      System.assertEquals('DR/'+branches.get(dr.branch__c).branch_code__c+'-'+String.valueOf(++branches.get(dr.branch__c).drautonumber__c),dr.name);
  }
}

It's fairly late here, so please take this post with a grain of salt (or coffee, perhaps). The main idea here is to outline how I would recommend that you write your trigger. It needs to actually perform a database insert in order to run the trigger. Your posted code does not perform any testing at all because it runs against an empty list of records.

 

Also note that the trigger has to run in "bulk" or you risk user exceptions when you perform imports, data migrations, etc.