You need to sign in to do that
Don't have an account?
HNT_Neo
Need help creating an apex class test from my Apex Class file
Hello,
Need some help with creating an Apex Test Class for this Apex Class I created which populates a custom object's country field's as either Canada or United States. I attempted to start creating the Apex Test Class but ended up not being able to create it.
This is my attempt in creating the Apex Test Class (generic Salesforce template):
Need some help with creating an Apex Test Class for this Apex Class I created which populates a custom object's country field's as either Canada or United States. I attempted to start creating the Apex Test Class but ended up not being able to create it.
global class PostalCodeUtils { public static String getPCCountry(String postalCode){ if (postalCode == null || postalCode.length() <= 1){ return 'United States'; } if (!postalCode.trim().left(1).isNumeric()) { return 'Canada'; } else { return 'United States'; } } public static String getPCID(String postalCode, String pcCountry){ if (postalCode == null){ return ''; } if (pcCountry == 'Canada'){ if (postalCode.length() >= 3){ return postalCode.substring(0, 3).toUpperCase(); } } else { if (postalCode.length() >= 5){ return postalCode.substring(0, 5); } } return postalCode; } }
This is my attempt in creating the Apex Test Class (generic Salesforce template):
@isTest private class PostalCodeUtilss { @isTest static void test_method_one() { // Implement test code } @isTest static void test_method_two() { // Implement test code } }Any ideas? Please let me know.
Everyone is slightly different in writing test code and follow slightly different "rules". I like to have a test method for each method in the class. This is so any change to the method only affects a single test method. I change the method, i just have to adjust the matching test method. I also like to write separate test methods for each branch, (IF...ELSE..) however, for this simple utility I would not go to that extent.
So my test class would look like: Remember that writing a class that simply covers your code should not be enough. You should be confident that the code does what you expect. Does stuff get converted to Uppercase, handle bad data, exceptions, etcI would also test where the data is not quite what is expected. Hence, I would test a short postcode, a longer postcode etc. I noticed your class has a IF with 2 options , so i would write tests for each option.
Hope that provides some insights.
Regards
Andrew G
All Answers
Please check the below Code:
Please mark this as the best answer if you find it positive.
Thanks
Jolly Birdi.
Everyone is slightly different in writing test code and follow slightly different "rules". I like to have a test method for each method in the class. This is so any change to the method only affects a single test method. I change the method, i just have to adjust the matching test method. I also like to write separate test methods for each branch, (IF...ELSE..) however, for this simple utility I would not go to that extent.
So my test class would look like: Remember that writing a class that simply covers your code should not be enough. You should be confident that the code does what you expect. Does stuff get converted to Uppercase, handle bad data, exceptions, etcI would also test where the data is not quite what is expected. Hence, I would test a short postcode, a longer postcode etc. I noticed your class has a IF with 2 options , so i would write tests for each option.
Hope that provides some insights.
Regards
Andrew G