Someone plz help me how to write test method for below method

public static Integer getFolderIndex(String frup_folderName, String recordName){
   Integer  folderIndex = 0;

   if ( !frup_folderName.equalsignorecase(recordName)) {
        folderIndex = integer.valueof(frup_folderName.substringAfter(recordName+'_'));
   system.debug('folder index'+folderIndex);
   return folderIndex;

What I have written

static testMethod void  getFolderIndex(){ 
   Integer  folderIndex = 0;
   Integer objectLabel2 = UtilityClass.getFolderIndex('Folder213','Recoed1');

I am getting following error

System.TypeException: Invalid integer:

1 Answer 1


Have a look at this line of code

folderIndex = integer.valueof(frup_folderName.substringAfter(recordName+'_'));

and you are passing in the values 'Folder213' and 'Recoed1'

so what will be the result of frup_folderName.substringAfter(recordName+'_') ?

It will look for "Recoed1_" within the string "Folder213" and return the text after that. Since the search string doesn't exist within the initial string, it will return blank which cannot be evaluated to an Integer, hence your problem.

You might want to break this down to show the problem better

String temp = frup_folderName.substringAfter(recordName+'_');
if (temp.isNumeric()) {
   folderIndex = integer.valueof(temp);
} else {
   // handle your error

Your test might be intended to look something like this:

Integer objectLabel2 = UtilityClass.getFolderIndex('MyPrefix_213','MyPrefix');
  • Oh!! yeah you are correct. Silly me. Thank you for your help :) Jan 8, 2014 at 12:41

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .