+ Start a Discussion
Bishara Shwayhat 10Bishara Shwayhat 10 

NumberTOWordConvertion test class

as im not a coder, can someone help me writing a test class for this class :
public class NumberTOWordConvertion {

    // Call this method with Number to convert
    public String getNumberTOWordConvertion(Decimal num) {

        Decimal junkVal = num;
        Decimal junkValFiles = junkVal - Math.floor(junkVal);
        junkVal = Math.floor(junkVal);

        String obStr = junkVal.toPlainString();
        String[] numReversed = obStr.split('');
        String[] actnumber = reverse(numReversed);
        String firstHalf = convertInWords(numReversed, actnumber);

        Integer tmp = Math.round(junkValFiles * 100);
        junkValFiles = (Decimal)tmp / 100; System.debug('jj :' + junkValFiles);
        String paisaStr = junkValFiles.toPlainString();
        String secondHalf;
        if (paisaStr == '0') {
            secondHalf = '';
        } else if (paisaStr.length() != 4) {
            paisaStr = paisaStr + '0';
            paisaStr = paisaStr.substring(2);
            String [] numReversedFiles = paisaStr.split('');
            String[] actnumberFiles = reverse(numReversedFiles);
            secondHalf = convertInWords(numReversedFiles, actnumberFiles);
        } else {
            paisaStr = paisaStr.substring(2);
            String [] numReversedFiles = paisaStr.split('');
            String[] actnumberFiles = reverse(numReversedFiles);
            secondHalf = convertInWords(numReversedFiles, actnumberFiles);
        }

        String SumOFHalves = '';

        if (secondHalf.length() > 4) {
            firstHalf = firstHalf.replace('Only', 'JDs And ');
            secondHalf = secondHalf.replace('Only', 'Files Only');
            SumOFHalves = firstHalf + secondHalf;
        } else {
            firstHalf = firstHalf.replace('Only', 'JDs Only');
            SumOFHalves = firstHalf;
        }

        // IF amount has any value
        if (SumOFHalves.length() > 5) {
            return SumOFHalves;
        } else {
            return '';
        }
    }
    // Method reverse the number
    public List<String> reverse(List<String> strToRev) {
        List<String> revList = new List<String>();
        for (Integer i = strToRev.size() - 1; i >= 0; i--) {
            revList.add(strToRev.get(i));
        }
        revList.add('');
        return revList;
    }

    public String convertInWords(String[] numRev, String[] actnum) {
        List<String> iWords = new List<String> {'Zero', ' One', ' Two', ' Three', ' Four', ' Five', ' Six', ' Seven', ' Eight', ' Nine'};
        List<String> ePlace = new List<String> {' Ten', ' Eleven', ' Twelve', ' Thirteen', ' Fourteen', ' Fifteen', ' Sixteen', ' Seventeen', ' Eighteen', ' Nineteen'};
        List<String> tensPlace = new List<String> {'dummy', ' Ten', ' Twenty', ' Thirty', ' Forty', ' Fifty', ' Sixty', ' Seventy', ' Eighty', ' Ninety' };

        Integer iWordsLength = numRev.size();
        String totalWords = '';
        List<String> inWords = new List<String>();
        for (Integer k = 0; k < iWordsLength; k++) {
            inWords.add('');
        }
        String finalWord = '';
        Integer j = 0;

        // Main For loop
        for (Integer i = 0; i < iWordsLength; i++) {

            if (i == 0) {
                if (actnum[i] == '0' || actnum[i + 1] == '1') {
                    inWords[j] = '';
                } else {
                    inWords[j] = iWords[Integer.valueof(actnum[i])];
                }
                inWords[j] = inWords[j] + ' Only';
            } else if (i == 1) {

                if (actnum[i] == '0') {
                    inWords[j] = '';
                } else if (actnum[i] == '1') {
                    inWords[j] = ePlace[Integer.valueof(actnum[i - 1])];
                } else {
                    inWords[j] = tensPlace[Integer.valueof(actnum[i])];
                }
            } else if (i == 2) {
                if (actnum[i] == '0') {
                    inWords[j] = '';
                } else if (actnum[i - 1] != '0' && actnum[i - 2] != '0') {
                    inWords[j] = iWords[Integer.valueof(actnum[i])] + ' Hundred and';
                } else {
                    inWords[j] = iWords[Integer.valueof(actnum[i])] + ' Hundred';
                }
            } else if (i == 3) {
                if (actnum[i] == '0' || actnum[i + 1] == '1') {
                    inWords[j] = '';
                } else {
                    inWords[j] = iWords[Integer.valueof(actnum[i])];
                }
                if (actnum[i + 1] != '0' || Integer.valueof(actnum[i]) > 0) {
                    inWords[j] = inWords[j] + ' Thousand';
                }
            } else if (i == 4) {

                if (actnum[i] == '0') {
                    inWords[j] = '';
                } else if (actnum[i] == '1') {
                    inWords[j] = ePlace[Integer.valueof(actnum[i - 1])];
                } else {
                    inWords[j] = tensPlace[Integer.valueof(actnum[i])];
                }

            } else if (i == 5) {
                if (actnum[i] == '0' || actnum[i + 1] == '1') {
                    inWords[j] = '';
                } else {
                    inWords[j] = iWords[Integer.valueof(actnum[i])];
                }
                if (actnum[i + 1] != '0' || Integer.valueof(actnum[i]) > 0) {
                    inWords[j] = inWords[j] + ' Lakh';
                }
            } else if (i == 6) {

                if (actnum[i] == '0') {
                    inWords[j] = '';
                } else if (actnum[i] == '1') {
                    inWords[j] = ePlace[Integer.valueof(actnum[i - 1])];
                } else {
                    inWords[j] = tensPlace[Integer.valueof(actnum[i])];
                }

            } else if (i == 7) {
                if (actnum[i] == '0' || actnum[i + 1] == '1' ) {
                    inWords[j] = '';
                } else {
                    inWords[j] = iWords[Integer.valueof(actnum[i])];
                }
                inWords[j] = inWords[j] + ' Crore';
            } else if (i == 8) {

                if (actnum[i] == '0') {
                    inWords[j] = '';
                } else if (actnum[i] == '1') {
                    inWords[j] = ePlace[Integer.valueof(actnum[i - 1])];
                } else {
                    inWords[j] = tensPlace[Integer.valueof(actnum[i])];
                }

            }

            j++;
        }
        // End of For loop

        // Reverse the List
        inWords = reverse(inWords);

        for (Integer i = 0; i < inWords.size(); i++) {
            finalWord += inWords[i];
        }

        return finalWord;
    }


}

thnx in advance
Best Answer chosen by Bishara Shwayhat 10
Soyab HussainSoyab Hussain
Hi Bishara Shwayhat,

Try this code may be this will cover your class and trigger code also.
@isTest
public class NumberTOWordConvertionTest {
    @testSetup static void setup() {
        Request__c req = new Request__c (Payable_To__c = 'test', Invoice_Date__c = Date.Today(), 
                                        Invoice_Num__c = '123', Explanation__c = 'test');
        insert req;      
        Request_Details__c rqDetails = new Request_Details__c();
        rqDetails.Budget_Line_Item__c = getPicklistValue('Request_Details__c', 'Budget_Line_Item__c');  
        rqDetails.RefNum__c = req.Id;
        rqDetails.Amount__c = 12; 
        rqDetails.Account_Name__c = 'test';
        insert rqDetails;
        update req;
    }
    @isTest
    private static void testMethod1(){
        NumberTOWordConvertion numWordObject = new NumberTOWordConvertion();
        numWordObject.getNumberTOWordConvertion(12345.89);
        numWordObject.getNumberTOWordConvertion(123456789);
    }
    private static String getPicklistValue(String ObjectApi_name,String Field_name){ 
      Schema.SObjectType targetType = Schema.getGlobalDescribe().get(ObjectApi_name);
      Sobject Object_name = targetType.newSObject();
      Schema.sObjectType sobject_type = Object_name.getSObjectType(); //grab the sobject that was passed
      Schema.DescribeSObjectResult sobject_describe = sobject_type.getDescribe(); //describe the sobject
      Map<String, Schema.SObjectField> field_map = sobject_describe.fields.getMap(); 
      List<Schema.PicklistEntry> pick_list_values = field_map.get(Field_name).getDescribe().getPickListValues(); 
      return pick_list_values[0].getValue();  
    }
}
I hope you find the above solution helpful. If it does, please mark as Best Answer.


Regards,
Soyab

All Answers

Soyab HussainSoyab Hussain
Hi Bishara Shwayhat,

Try this code it will cover your class up to 76%.
@isTest
public class NumberTOWordConvertionTest {
	@isTest
    private static void testMethod1(){
        NumberTOWordConvertion numWordObject = new NumberTOWordConvertion();
        numWordObject.getNumberTOWordConvertion(12345);
        numWordObject.getNumberTOWordConvertion(123456789);
    }
}

If you have any confusion, you can Email me at:
soyab.hussain21@gmail.com

Regards,
Soyab
 
Deepali KulshresthaDeepali Kulshrestha
Hi Bishara, 

Try these code your code coverage is 83%.


@isTest
public class TestDemoTest {
    @isTest
    private static void testDemo(){
        TestDemo td = new TestDemo();
        Test.startTest();
        td.getNumberTOWordConvertion(12345.89);
        td.getNumberTOWordConvertion(123456789);
        Test.stopTest();
       
    }
}


I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha
www.kdeepali.com
Bishara Shwayhat 10Bishara Shwayhat 10
@Soyab Hussain 
i tried it and it gave 76%, but when i checked my trigger its still 0%.
this is my trigger:

trigger convertNumbersToWords on Request__c (before Insert, Before Update) { 
   NumberTOWordConvertion ntoWord = new NumberTOWordConvertion();
    For(Request__c T : trigger.new){
        if(T.Total_Amount__c>0){

         String numbertoWord = ntoWord.getNumberTOWordConvertion(T.Total_Amount__c);
         T.Words__c = numbertoWord;
          
        }
    }
}
Soyab HussainSoyab Hussain
Hi Bishara Shwayhat,

I have to know about your Request__c SObject fields, after that I can cover your trigger code.

But you can try this code may be this will cover your class and trigger code also.
 
@isTest
public class NumberTOWordConvertionTest {
    @testSetup static void setup() {
        Request__c req = new Request__c (Total_Amount__c = 12);
        insert req;
    }
	@isTest
    private static void testMethod1(){
        NumberTOWordConvertion numWordObject = new NumberTOWordConvertion();
        numWordObject.getNumberTOWordConvertion(12345.89);
        numWordObject.getNumberTOWordConvertion(123456789);
    }
}
If you have any confusion, you can Email me at:
soyab.hussain21@gmail.com

Regards,
Soyab
 
Bishara Shwayhat 10Bishara Shwayhat 10
Hey Soyab
Do u need to know all the fields or mandatory fields only.. Plus note that the total amount field is a roll up summary field. 
Thank you
 
Soyab HussainSoyab Hussain
Hi Bishara Shwayhat,

I need to know only mandatory fields with their data types.
Bishara Shwayhat 10Bishara Shwayhat 10
Hi Soyab
in the primary object (Request__c) :
Date
Payable_To__c (text),
Invoice_Date__c(date),
Invoice_Num__c(Text),
Explanation__c(Text)
and the rollup summary field Total_Amount__c

the child object (Request_Details__c:
Budget_Line_Item__c (Picklist)
RefNum__c Master-Detail(Request)
Amount__c Number(10,3)
Account_Name__c Text(255)

​​​​​​​Thank you
Soyab HussainSoyab Hussain
Hi Bishara Shwayhat,

Try this code may be this will cover your class and trigger code also.
@isTest
public class NumberTOWordConvertionTest {
    @testSetup static void setup() {
        Request__c req = new Request__c (Payable_To__c = 'test', Invoice_Date__c = Date.Today(), 
                                        Invoice_Num__c = '123', Explanation__c = 'test');
        insert req;      
        Request_Details__c rqDetails = new Request_Details__c();
        rqDetails.Budget_Line_Item__c = getPicklistValue('Request_Details__c', 'Budget_Line_Item__c');  
        rqDetails.RefNum__c = req.Id;
        rqDetails.Amount__c = 12; 
        rqDetails.Account_Name__c = 'test';
        insert rqDetails;
        update req;
    }
    @isTest
    private static void testMethod1(){
        NumberTOWordConvertion numWordObject = new NumberTOWordConvertion();
        numWordObject.getNumberTOWordConvertion(12345.89);
        numWordObject.getNumberTOWordConvertion(123456789);
    }
    private static String getPicklistValue(String ObjectApi_name,String Field_name){ 
      Schema.SObjectType targetType = Schema.getGlobalDescribe().get(ObjectApi_name);
      Sobject Object_name = targetType.newSObject();
      Schema.sObjectType sobject_type = Object_name.getSObjectType(); //grab the sobject that was passed
      Schema.DescribeSObjectResult sobject_describe = sobject_type.getDescribe(); //describe the sobject
      Map<String, Schema.SObjectField> field_map = sobject_describe.fields.getMap(); 
      List<Schema.PicklistEntry> pick_list_values = field_map.get(Field_name).getDescribe().getPickListValues(); 
      return pick_list_values[0].getValue();  
    }
}
I hope you find the above solution helpful. If it does, please mark as Best Answer.


Regards,
Soyab
This was selected as the best answer
Bishara Shwayhat 10Bishara Shwayhat 10
hi soyab,
i got this  Error: Compile Error: Invalid type: Request_Details__c at line 7 column 9

thank you
Soyab HussainSoyab Hussain
Hi Bishara,
Check your Salesforce object API name that you gave me. (Request_Details__c )
Bishara Shwayhat 10Bishara Shwayhat 10
sorry for that i fixed it and i saved it but when i run test i received this erroe :
System.NullPointerException: Attempt to de-reference a null object

thank you
 
Soyab HussainSoyab Hussain
Please give me line number of error.
and give me Budget_Line_Item__c field picklist values.
Soyab HussainSoyab Hussain
And you have to change salesforce object API name here also at line number 08.
User-added image
Bishara Shwayhat 10Bishara Shwayhat 10
I changed it now and I made a run test but 0/1 passed
The list of budget line is only number 1 
Soyab HussainSoyab Hussain
Sorry I did not understand.
Bishara Shwayhat 10Bishara Shwayhat 10
I changed the object request_details__c to request_detail__c.. And then I made new run to test the class in the results it gave me (0/1)methods passed