+ Start a Discussion
subodh chaturvedi 17subodh chaturvedi 17 

how to write the test class Of a helper class

I have my helper Class But i am not getting how to write the test class Of it.

public class BitSet
 {
   public Map < String, Integer > alphaNumCharCodes {
    get;
    set;
 }
  public Map < String, Integer > base64CharCodes
 {
    get;
    set;
 }

  public BitSet() {
  LoadCharCodes();
 }
 
 //Method loads the character codes for all letters
 public void LoadCharCodes() 
{
  alphaNumCharCodes = new Map < String, Integer > {
   'A' => 65,
   'B' => 66,
   'C' => 67,
   'D' => 68,
   'E' => 69,
   'F' => 70,
   'G' => 71,
   'H' => 72,
   'I' => 73,
   'J' => 74,
   'K' => 75,
   'L' => 76,
   'M' => 77,
   'N' => 78,
   'O' => 79,
   'P' => 80,
   'Q' => 81,
   'R' => 82,
   'S' => 83,
   'T' => 84,
   'U' => 85,
   'V' => 86,
   'W' => 87,
   'X' => 88,
   'Y' => 89,
   'Z' => 90
  };
  base64CharCodes = new Map < String, Integer > ();
  //all lower cases
  Set < String > pUpperCase = alphaNumCharCodes.keySet();
  for (String pKey: pUpperCase) 
  {
   //the difference between upper case and lower case is 32
   alphaNumCharCodes.put(pKey.toLowerCase(), alphaNumCharCodes.get(pKey) + 32);
   //Base 64 alpha starts from 0 (The ascii charcodes started from 65)
   base64CharCodes.put(pKey, alphaNumCharCodes.get(pKey) - 65);
   base64CharCodes.put(pKey.toLowerCase(), alphaNumCharCodes.get(pKey) - (65) + 26);
  }
  //numerics
  for (Integer i = 0; i <= 9; i++) 
  {
   alphaNumCharCodes.put(string.valueOf(i), i + 48);
   //base 64 numeric starts from 52
   base64CharCodes.put(string.valueOf(i), i + 52);
  }
}
 
 public List < Integer > testBits(String pValidFor, List < Integer > nList) 
 {
    List < Integer > results = new List < Integer > ();
    List < Integer > pBytes = new List < Integer > ();
    Integer bytesBeingUsed = (pValidFor.length() * 6) / 8;
    Integer pFullValue = 0;
        if (bytesBeingUsed <= 1)
            return results;
        for (Integer i = 0; i < pValidFor.length(); i++) 
        {
            pBytes.Add((base64CharCodes.get((pValidFor.Substring(i, i + 1)))));
        }
    for (Integer i = 0; i < pBytes.size(); i++) 
    {
        Integer pShiftAmount = (pBytes.size() - (i + 1)) * 6; //used to shift by a factor 6 bits to get the value
        pFullValue = pFullValue + (pBytes[i] << (pShiftAmount));
    }
 
        Integer bit;
        Integer targetOctet;
        Integer shiftBits;
        Integer tBitVal;
        Integer n;
        Integer nListSize = nList.size();
  for (Integer i = 0; i < nListSize; i++) 
  {
        n = nList[i];
        bit = 7 - (Math.mod(n, 8));
        targetOctet = (bytesBeingUsed - 1) - (n >> bytesBeingUsed);
        shiftBits = (targetOctet * 8) + bit;
        tBitVal = ((Integer)(2 << (shiftBits - 1)) & pFullValue) >> shiftBits;
    if (tBitVal == 1)
        results.add(n);
  }
        return results;
 }
}
v varaprasadv varaprasad
Hi Subodh,


Please check once below sample code.
@isTest
private class BitSet_Test{
  static testMethod void test_LoadCharCodes_UseCase1(){
  public Map < String, Integer > alphaNumCharCodes =  new Map < String, Integer > {
   'A' => 65,
   'B' => 66,
   'C' => 67,
   'D' => 68,
   'E' => 69,
   'F' => 70,
   'G' => 71,
   'H' => 72,
   'I' => 73,
   'J' => 74,
   'K' => 75,
   'L' => 76,
   'M' => 77,
   'N' => 78,
   'O' => 79,
   'P' => 80,
   'Q' => 81,
   'R' => 82,
   'S' => 83,
   'T' => 84,
   'U' => 85,
   'V' => 86,
   'W' => 87,
   'X' => 88,
   'Y' => 89,
   'Z' => 90
  };
  
    BitSet obj01 = new BitSet();
    obj01.LoadCharCodes();
  }
  static testMethod void test_testBits_UseCase1(){
  public Map < String, Integer > alphaNumCharCodes =  new Map < String, Integer > {
   'A' => 65,
   'B' => 66,
   'C' => 67,
   'D' => 68,
   'E' => 69,
   'F' => 70,
   'G' => 71,
   'H' => 72,
   'I' => 73,
   'J' => 74,
   'K' => 75,
   'L' => 76,
   'M' => 77,
   'N' => 78,
   'O' => 79,
   'P' => 80,
   'Q' => 81,
   'R' => 82,
   'S' => 83,
   'T' => 84,
   'U' => 85,
   'V' => 86,
   'W' => 87,
   'X' => 88,
   'Y' => 89,
   'Z' => 90
  };
    BitSet obj01 = new BitSet();
    List<Integer>() lstofInts = new List<Integer>();
    obj01.testBits('test data',lstofInts);
  }
}

Hope it helps you.
Please let me know in case of any other assistance.


Thanks
Varaprasad 


 
v varaprasadv varaprasad
Use below line instead of 
List<Integer>() lstofInts = new List<Integer>();

 
List<Integer> lstofInts = new List<Integer>{65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,
    80,81,82,83,84,85,86,87,88,89,90};
system.debug('==lstofInts=='+lstofInts);

Hope it helps you .

Thanks
Varaprasad 
subodh chaturvedi 17subodh chaturvedi 17
Hi  v varaprasad ,

It is giving only total 58% for  test_LoadCharCodes_UseCase1() method & 76% for test_testBits_UseCase1()  method & over all percentage is 76% we want to deploy with more than that.
v varaprasadv varaprasad
Hi Subodh,

Please check once below code now.

 
@isTest
public class BitSet_Test{
    static testMethod void test_LoadCharCodes_UseCase1(){
         Map < String, Integer > alphaNumCharCodes =  new Map < String, Integer > {
            'A' => 65,
                'B' => 66,
                'C' => 67,
                'D' => 68,
                'E' => 69,
                'F' => 70,
                'G' => 71,
                'H' => 72,
                'I' => 73,
                'J' => 74,
                'K' => 75,
                'L' => 76,
                'M' => 77,
                'N' => 78,
                'O' => 79,
                'P' => 80,
                'Q' => 81,
                'R' => 82,
                'S' => 83,
                'T' => 84,
                'U' => 85,
                'V' => 86,
                'W' => 87,
                'X' => 88,
                'Y' => 89,
                'Z' => 90
                };
                    
                    BitSet obj01 = new BitSet();
        obj01.LoadCharCodes();
    }
    static testMethod void test_testBits_UseCase1(){
         Map < String, Integer > alphaNumCharCodes =  new Map < String, Integer > {
            'A' => 65,
                'B' => 66,
                'C' => 67,
                'D' => 68,
                'E' => 69,
                'F' => 70,
                'G' => 71,
                'H' => 72,
                'I' => 73,
                'J' => 74,
                'K' => 75,
                'L' => 76,
                'M' => 77,
                'N' => 78,
                'O' => 79,
                'P' => 80,
                'Q' => 81,
                'R' => 82,
                'S' => 83,
                'T' => 84,
                'U' => 85,
                'V' => 86,
                'W' => 87,
                'X' => 88,
                'Y' => 89,
                'Z' => 90
                };
                    BitSet obj01 = new BitSet();
        List<Integer> lstofInts = new List<Integer>{65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,
            80,81,82,83,84,85,86,87,88,89,90};
                system.debug('==lstofInts=='+lstofInts);
        obj01.testBits('testdata',lstofInts);
        
    }
}

Hope it helps you.
Please let me know in case of any other assistance.


Thanks
Varaprasad
 
subodh chaturvedi 17subodh chaturvedi 17
Hi v varaprasad,

The  test_LoadCharCodes_UseCase1() method covers Only 58% & test_testBits_UseCase1 method covers 98%. & one more issue while i am deploying this Test class to production it is Giving me  Error
  system.Null pointer Exception :Attempt to de-reference to a Null Object.
  Stack trace: class .BitSet.testBits line 83,Column 1, Class.BitSet_Test.test_testBits_Usecase 1: line 69,Column 1
subodh chaturvedi 17subodh chaturvedi 17
The issue Is Resolved the Error Is coming Because I have a dependent Picklist But it Is Not present in the production so i create that Picklist & made dependent & then Deploy the classes It Deployed Successfully.
v varaprasadv varaprasad
hi Subodh,

Please mark it as best answer if my information helps you,

Thanks
Varaprasad