• vinod kumar 364
  • NEWBIE
  • 10 Points
  • Member since 2017

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 6
    Questions
  • 4
    Replies
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;
  }
   
    
    
}



 
User-added image
please look at my above Image it shows how my page is breaking on my Visualforce PDF page, Here I want my entire 3rd row should go to next page without breaking in the middle.

So please find the code related to above concern:
<table style="font-size:80%;*page-break-inside:avoid*" border="1px" cellpadding="0" cellspacing="0" height="100%" width="100%​">

 
I tried several times to get the code coverage for the below test class now I need someone's help to get code coverage for red color coding in the Image How can I ???
Here the below image shows the code coverage.​

code coverage problem

My trigger
trigger customsolinsert on Price_Study__c (after update, after insert) {
// Find the existing (0 or 1) Custom_solution__c that reference Price_Study__c 
    Map<Id, Custom_solution__c> m = new Map<Id, Custom_solution__c>();
   list<Custom_solution__c> oblist=[ select id, Price_Study__c from Custom_solution__c where Price_Study__c in :Trigger.newMap.keySet() ];
    for (Custom_solution__c ob : oblist) {
        m.put(ob.Price_Study__c, ob);
    }  
// Insert or update the Custom_solution__c
List<Custom_solution__c> csol= new List<Custom_solution__c>();

for (Price_Study__c b : trigger.new) {
    // Get record to update
    Custom_solution__c ob = m.get(b.id);

  if(b.Analysis_Done_By__c<>Null){
 if (ob == null) {
        // If no record to update, add a record to be inserted
        ob = new Custom_solution__c(Price_Study__c = b.id);
    }
         ob.Price_Study__c=b.Id;       
         ob.Country__c=b.Country_of_Treatment__c;
         ob.Who_Deals__c=b.Analysis_Done_By__c;
         ob.Customer__c=b.Account_Name__c;
         ob.Product_line__c=b.Product_line__c;
         ob.Function_Focus__c=b.Function_Focus__c;
         ob.Comments__c=b.Generic_Comments__c;
         ob.Nbrs_Post__c=b.Nbrs_Post__c;
         ob.Series_Row_nbr__c=b.Series_Row_nbr__c;

    csol.add(ob);
   }
} 
upsert csol;

Test class
@istest(isParallel=true)
  public class Testcustomsolinsert{
  @istest Static void Testcustomsol1(){

    Profile prof = [select id from profile where name='system Administrator'];
    List<User> lstUser = [Select u.Profile.Name, u.ProfileId, u.IsActive, u.Id From User u Where IsActive = true AND Profile.Name = 'System Administrator'];
    system.runAs(lstUser[0]){


   Account acc=new account(Name='NicoTestacc',BillingCountry='India');
    insert acc;

    Opportunity op1= new opportunity(Name='NicoTestOpp',CloseDate=date.today(),StageName='Qualification',Product_Type__c='DPI',Accountid=acc.Id);
    insert op1;
    System.assertEquals(op1.name,'NicoTestOpp');
    Price_Study__c ps=new Price_Study__c(Country_of_Treatment__c='India',Series_Row_nbr__c=3,
                                   Nbrs_Post__c=2,Analysis_Done_By__c=op1.OwnerId,
                                   Generic_Comments__c='asdfds',Function_Focus__c='sdad',
                                   Product_line__c='CMM');

    Insert Ps;

        Map<Id, Custom_solution__c> m = new Map<Id, Custom_solution__c>();             


        Custom_solution__c cs1=new Custom_solution__c(Price_Study__c=PS.Id,Country__c=Ps.Country_of_Treatment__c);

        Insert cs1;

        delete cs1;

     Custom_solution__c cs=new Custom_solution__c();


          cs.Price_Study__c=ps.Id;
          cs.Country__c=Ps.Country_of_Treatment__c;
          cs.Who_Deals__c=ps.Analysis_Done_By__c;
          cs.Customer__c=ps.Account_Name__c;
          cs.Product_line__c='Cmm';
          cs.Function_Focus__c=ps.Function_Focus__c;
          cs.Comments__c=ps.Generic_Comments__c;
          cs.Nbrs_Post__c=ps.Nbrs_Post__c;
          cs.Series_Row_nbr__c=ps.Series_Row_nbr__c;

  Insert cs;

        cs.Function_Focus__c=ps.Function_Focus__c;

  Update cs;
    }      
}
  }