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
April Barclay 8April Barclay 8 

Test code coverage help

I have some code a third party put in with zero test coverage.  I do not know how to write test coverage.  Any ideas?  I need 2% more coverage to meet the minimum of 75%.
Best Answer chosen by April Barclay 8
Marcilio SouzaMarcilio Souza
April,

Here the class test you can use. Do you know how to execute through the developer console?
 
@isTest(SeeAllData=true)
public class TestMeritIncreaseCalculationUtility {
    
    static testMethod void getNewRate(){
        MeritIncreaseCalculationUtility objTest = new MeritIncreaseCalculationUtility();
        
        //Here you put parameters to cover all situations of the method
        //This test cover 60% of this class
        objTest.getNewRate(0, 6, 20);     
        objTest.getNewRate(4, 2, 50);
    }
}
Please if this help you put like best anwser.
Marcilio.

Send me a e-mail if you want more help
marcilioleites@gmail.com
 

All Answers

Marcilio SouzaMarcilio Souza
Hi,

You have to write, if you show me some classe i would help you with the test class.

Marcilio
April Barclay 8April Barclay 8
Thank you!!  Here is the one without any coverage:
/**
  * @ClassName: MeritIncreaseCalculationUtility
  * @Author: Prashanth Kambalapally    
  * @Description: 
  *         - Provides calculations for merit increase.
  * @CustomSettingsUsed: 
        - 
  * @TestClass: 
  * @VersionHistory:
  *         ~0.1 - 12-27-2013
  *            Original version
  **/
public with sharing class MeritIncreaseCalculationUtility {
  public String getNewRate(Decimal curRate,Decimal pRating,Decimal rangePenetration) {
    Decimal nRatePct=0.0,nRate=0.0,netInc=0.0;
    if (pRating != null && pRating < 6) {
      if(pRating == 4 || pRating == 5){
        nRatePct=0;
      }
      else if (pRating == 1) {
        if(rangePenetration<0){nRatePct=3;}
        else if(rangePenetration >=0 && rangePenetration <= 25) { nRatePct = 2.75; }
        else if (rangePenetration >25  && rangePenetration <= 50) { nRatePct = 2.50; }
        else if (rangePenetration > 50 && rangePenetration <= 75) { nRatePct = 2.25; }
        else if (rangePenetration > 75 && rangePenetration < 100) { nRatePct = 1.75; }
        else if (rangePenetration >= 100) { nRatePct = 0; }
      }
      else if (pRating == 2) {
        if(rangePenetration<0){nRatePct=2.75;}
        else if(rangePenetration >=0 && rangePenetration <= 25) { nRatePct = 2.5; }
        else if (rangePenetration >25  && rangePenetration <= 50) { nRatePct = 2.25; }
        else if (rangePenetration > 50 && rangePenetration <= 75) { nRatePct = 2.00; }
        else if (rangePenetration > 75 && rangePenetration < 100) { nRatePct = 1.5; }
        else if (rangePenetration >= 100) { nRatePct = 0; }
      }
      else if(pRating == 3) {
        if(rangePenetration<0){nRatePct=2.5;}
        else if(rangePenetration >=0 && rangePenetration <= 25) { nRatePct = 2.25; }
        else if (rangePenetration >25  && rangePenetration <= 50) { nRatePct = 2.0; }
        else if (rangePenetration > 50 && rangePenetration <= 75) { nRatePct = 1.75; }
        else if (rangePenetration > 75 && rangePenetration < 100) { nRatePct = 1.25; }
        else if (rangePenetration >= 100) { nRatePct = 0; }
      }
      
      if (nRatePct > 0) {
        Decimal incVal = (curRate*(nRatePct/100));
        nRate = (curRate + incVal).setScale(3);
      } else {
        nRate =curRate;
      }
        netInc = (nRate - curRate);
        if(nRate >0.0){
          nRate = Decimal.valueOf(round_decimals(nRate, 2));
        }
        if(netInc>0.0){
          netInc = Decimal.valueOf(round_decimals(netInc, 2));
        }
      
      System.debug('vals======='+nRatePct+';'+nRate+';'+netInc);
      return nRatePct+';'+nRate+';'+netInc;
    } else {
      return 'Please enter a Valid Performance Rating (0-5)';
    }
  }//end getNewRate()
  
  /*
  public String getNewRate(Decimal curRate,Decimal pRating,Decimal rangePenetration) {
    Decimal nRatePct=0.0,nRate=0.0,netInc=0.0;
    if (pRating != null && pRating < 6) {
      if (rangePenetration > 0) {
        if (pRating == 4 || pRating == 5 || pRating == 0) {
          nRatePct = 0;
        }
        else if (pRating == 1) {
          if(rangePenetration > 1.0 && rangePenetration <= 90.5) { nRatePct = 1.95; }
          else if (rangePenetration >90.5  && rangePenetration <= 100.5) { nRatePct = 2.15; }
          else if (rangePenetration > 100.5 && rangePenetration <= 110.5) { nRatePct = 1.70; }
          else if (rangePenetration > 110.5 && rangePenetration <= 120.5) { nRatePct = 1.45; }
          else if (rangePenetration > 120.5 && rangePenetration <= 125) { nRatePct = 1.00; }
          else if (rangePenetration > 125) { nRatePct = 0; }
        }
        else if (pRating == 2) {
          if(rangePenetration > 1.0 && rangePenetration <= 90.5) { nRatePct = 1.50; }
          else if (rangePenetration > 90.5 && rangePenetration <= 100.5) { nRatePct = 1.75; }
          else if (rangePenetration > 100.5 && rangePenetration <= 110.5) { nRatePct = 1.25; }
          else if (rangePenetration > 110.5 && rangePenetration <= 120.5) { nRatePct = 1.00; }
          else if (rangePenetration > 120.5 && rangePenetration <= 125) { nRatePct = 1.00; }
          else if (rangePenetration > 125) { nRatePct = 0; }
        }
        else if(pRating == 3) {
          if(rangePenetration > 1.0 && rangePenetration <= 90.5) { nRatePct = 1.25; }
          else if (rangePenetration > 90.5 && rangePenetration <= 100.5) { nRatePct = 1.50; }
          else if (rangePenetration > 100.5 && rangePenetration <= 110.5) { nRatePct = 1.00; }
          else if (rangePenetration > 110.5 && rangePenetration <= 120.5) { nRatePct = 1.00; }
          else if (rangePenetration > 120.5 && rangePenetration <= 125) { nRatePct = 1.00; }
          else if (rangePenetration > 125) { nRatePct = 0; }
        }
        if (nRatePct > 0) {
          Decimal incVal = (curRate*(nRatePct/100));
          nRate = (curRate + incVal).setScale(3);
        } else {
          nRate =curRate;
        }
          netInc = (nRate - curRate);
          if(nRate >0.0){
            nRate = Decimal.valueOf(round_decimals(nRate, 2));
          }
          if(netInc>0.0){
            netInc = Decimal.valueOf(round_decimals(netInc, 2));
          }
      }
      System.debug('vals======='+nRatePct+';'+nRate+';'+netInc);
      return nRatePct+';'+nRate+';'+netInc;
    } else {
      return 'Please enter a Valid Performance Rating (0-5)';
    }
  }//end getNewRate()
  */
  public String round_decimals(Decimal original_number, Integer decimals) {
      Decimal result1 = original_number *((10.0).pow(decimals));
      Decimal result2 = Math.round(result1);
      Decimal result3 = result2 / (10.0).pow(decimals);
      return pad_with_zeros(result3, decimals);
  }//end round_decimals()
  
  public String pad_with_zeros(Decimal rounded_value, Integer decimal_places) {
      // Convert the number to a string
      String value_string = String.valueOf(rounded_value);
    
      // Locate the decimal point
      Integer decimal_location = value_string.indexOf('.');
      Integer decimal_part_length = 0;
      // Is there a decimal point?
      if(decimal_location == -1) {      
          // If no, then all decimal places will be padded with 0s
          decimal_part_length = 0;       
          // If decimal_places is greater than zero, tack on a decimal point
          value_string += decimal_places > 0 ? '.' : '';
      }
      else{
          // If yes, then only the extra decimal places will be padded with 0s
          decimal_part_length = value_string.length() - decimal_location - 1;
      }
      
      // Calculate the number of decimal places that need to be padded with 0s
      Decimal pad_total = decimal_places - decimal_part_length;    
      if(pad_total > 0) {        
          // Pad the string with 0s
          for(Integer counter = 1; counter <= pad_total; counter++){ 
              value_string += '0';
          }
        }//end if
      return value_string;
  }//end pad_with_zeros()
  
}
Marcilio SouzaMarcilio Souza
April,

Here the class test you can use. Do you know how to execute through the developer console?
 
@isTest(SeeAllData=true)
public class TestMeritIncreaseCalculationUtility {
    
    static testMethod void getNewRate(){
        MeritIncreaseCalculationUtility objTest = new MeritIncreaseCalculationUtility();
        
        //Here you put parameters to cover all situations of the method
        //This test cover 60% of this class
        objTest.getNewRate(0, 6, 20);     
        objTest.getNewRate(4, 2, 50);
    }
}
Please if this help you put like best anwser.
Marcilio.

Send me a e-mail if you want more help
marcilioleites@gmail.com
 
This was selected as the best answer
April Barclay 8April Barclay 8
Would I have to create this on the sandbox then promote via a change set?