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
vinod kumar 364vinod kumar 364 

Code coverage is not coming from the Apex class (apex class that i used in apex trigger)

Hi,

I have Problem with code coverage in apex class.

what I'm doing here is I wrote a trigger for convert currency to words in that trigger I used this apex class.

Now I just want to know how I want to write a test class for apex class? before I just wrote a test class for apex trigger for that apex class it is coming 68% of code coverage someone please help me how can I get code coverage for apex class.


Apex trigger 
trigger ConvertCurrencyToWords on I_Invoice__c (before insert, before update) {

    for (I_Invoice__c c: Trigger.new) {
    If(c.State_Code__c!='29'){
    
        if (c.Final_value_IGST__c!= null && c.Final_value_IGST__c>= 0) {
          
            Long n = c.Final_value_IGST__c.longValue();
            string amo = ConvrtCurrencyToWords.english_number(n);
            string amo1 = amo.remove(',');
            c.Amount_in_Words__c = amo1;
        } 
        
    }
    
     If(c.State_Code__c=='29'){
    
        if (c.Final_value_C_SGST__c!= null && c.Final_value_C_SGST__c>= 0) {
          
            Long n = c.Final_value_C_SGST__c.longValue();
            string amo = ConvrtCurrencyToWords.english_number(n);
            string amo1 = amo.remove(',');
            c.Amount_in_Words__c = amo1;
        } /*else {
            c.Amount_in_Words__c = null;
        } */
    }
    
    
   }
}

Apex class (which I used in apex trigger now I need code coverage for this apex class)
 
public with sharing class ConvrtCurrencyToWords { 
      
        static String[] to_19 = new string[]{ 'zero', 'One',  'Two', 'Three', 'Four',  'Five',  'Six', 'Seven',
                                              'Eight', 'Nine', 'Ten',  'Eleven', 'Twelve', 'Thirteen',  
                                              'Fourteen', 'Fifteen', 'Sixteen', 'Seventeen', 'Eighteen', 'Nineteen' }; 
        static String[] tens = new string[]{ 'Twenty', 'Thirty', 'Forty', 'Fifty', 'Sixty', 'Seventy', 'Eighty', 'Ninety'}; 
      
        static string[] denom = new string[]{ '', 
                                             'Thousand',   'Million',     'Billion',    'trillion',    'quadrillion',  
                                             'quintillion', 's!xtillion',   'septillion',  'octillion',   'nonillion',  
                                             'decillion',  'undecillion',   'duodecillion', 'tredecillion',  'quattuordecillion',  
                                             's!xdecillion', 'septendecillion', 'octodecillion', 'novemdecillion', 'vigintillion' }; 
    // convert a value < 100 to English.   
   public static string convert_nn(integer val) { 
             if (val < 20) 
        return to_19[val]; 
      if (val == 100) 
          return 'One Hundred'; 
      for (integer v = 0; v < tens.size(); v++) { 
        String dcap = tens[v]; 
        integer dval = 20 + 10 * v; 
        if (dval + 10 > val) { 
          if (Math.Mod(val,10) != 0) 
            return dcap + ' ' + to_19[Math.Mod(val,10)]; 
          return dcap; 
        }     
      } 
      return 'Should never get here, less than 100 failure'; 
    } 
    // convert a value < 1000 to english, special cased because it is the level that kicks   
    // off the < 100 special case. The rest are more general. This also allows you to  
    // get strings in the form of "forty-five hundred" if called directly.  
    public static String convert_nnn(integer val) { 
      string word = ''; 
      integer rem = val / 100; 
      integer mod = Math.mod(val,100); 
      if (rem > 0) { 
        word = to_19[rem] + ' Hundred and'; 
        if (mod > 0) { 
          word += ' '; 
        } 
      } 
      if (mod > 0) { 
        word += convert_nn(mod); 
      } 
      return word; 
    } 
    public static String english_number(long val) { 
      if (val < 100) { 
        return convert_nn(val.intValue()); 
      } 
      if (val < 1000) { 
        return convert_nnn(val.intValue()); 
      } 
      for (integer v = 0; v < denom.size(); v++) { 
        integer didx = v - 1; 
        integer dval = (integer)Math.pow(1000, v); 
        if (dval > val) { 
          integer mod = (integer)Math.pow(1000, didx); 
          integer l = (integer) val / mod; 
          integer r = (integer) val - (l * mod); 
          String ret = convert_nnn(l) + ' ' + denom[didx]; 
          if (r > 0) { 
            ret += ', ' + english_number(r); 
          } 
          return ret; 
        } 
      } 
      return 'Should never get here, bottomed out in english_number'; 
    } 
  }
Test class :
 
@istest
public class TestConvertCurrencyToWords {

  static testMethod void test() {
    
       Account acc=new Account();
       acc.Name='jsjsdd';
       acc.Industry__c='Energy';
       acc.Activity__c='End User';
       Acc.BillingCountry='India';
       Acc.BillingState='Sikkim';
       Acc.BillingCity='dsad';            
       Acc.BillingStreet='dsaf';             
       Acc.BillingPostalCode='453658';         
        
       Insert acc;
     
        I_Invoice__c co = new I_Invoice__c();
         co.Buyers_Order_No__c='blah';
         //co.Buyers_Order_Date__c='12-02-2018';
         co.Customer_GSTIN__c='14535HGFNHG423';
         co.Amount_in_Words__c='';
         co.State_Code__c='07';
         co.Despatch_Through__c='by hand';
         co.Part_Numbers__c=20;
         co.CGST__c=18;
         co.SGST__c=12;
         co.IGST__c=15;
         co.Freight_Insurance__c=50;
         co.Insurance__c=40;
         co.Packing_and_Forwarding_Charges__c=200;
         co.Account__c=acc.id;
         co.Autonum__c='4';
         co.Billing_Country__c=acc.BillingCountry;
         co.Billing_State_Province__c=acc.BillingState;
         co.Billing_City__c= Acc.BillingCity;
         co.Billing_Street__c= Acc.BillingStreet;
         co.Billing_Zip_Postal_Code__c= Acc.BillingPostalCode;
         co.Account_Name__c=Acc.Name;
         co.Freight_Insurance__c=200;
         co.Insurance__c=200;
         co.Amount_In_Words__c='DASDASD';
         co.Place_of_Supply__c='sada';
         co.Same_Address__c=true;
        insert co;
        co.Invoice_Generated__c=true;
        update co;
  }   

 static testMethod void test1() {
    
       Account acc=new Account();
       acc.Name='sdfsdf';
       acc.Industry__c='Energy';
       acc.Activity__c='End User';
       Acc.BillingCountry='India';
       Acc.BillingState='Sikkim';
       Acc.BillingCity='dsad';            
       Acc.BillingStreet='dsaf';             
       Acc.BillingPostalCode='453658';         
        
       Insert acc;
     
        I_Invoice__c co = new I_Invoice__c();
         co.Buyers_Order_No__c='blah';
         //co.Buyers_Order_Date__c='12-02-2018';
         co.Customer_GSTIN__c='14535HGFNHG423';
         co.State_Code__c='29';
         co.Amount_in_Words__c=null;
         
         co.Despatch_Through__c='by hand';
         co.Part_Numbers__c=20;
         co.CGST__c=18;
         co.SGST__c=12;
         co.IGST__c=15;
         co.Freight_Insurance__c=200;
         co.Insurance__c=200;
         co.Packing_and_Forwarding_Charges__c=200;
         co.Account__c=acc.id;
         co.Autonum__c='4';
         co.Billing_Country__c=acc.BillingCountry;
         co.Billing_State_Province__c=acc.BillingState;
         co.Billing_City__c= Acc.BillingCity;
         co.Billing_Street__c= Acc.BillingStreet;
         co.Billing_Zip_Postal_Code__c= Acc.BillingPostalCode;
         co.Account_Name__c=Acc.Name;
         co.Freight_Insurance__c=200;
         co.Insurance__c=200;
         co.Amount_In_Words__c='DASDASD';
         co.Place_of_Supply__c='sada';
         co.Same_Address__c=true;
        insert co;
        co.Invoice_Generated__c=true;
        update co;
  }
   
    
    
}



 
vinod kumar 364vinod kumar 364
Please find Code coverage of apex class(how i can get 100% code coverage for apex class)