+ Start a Discussion
HNT_NeoHNT_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. 
 
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. 

 
Best Answer chosen by HNT_Neo
Andrew GAndrew G

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. 

I 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.
 

if (postalCode == null ||  postalCode.length() <= 1){
So my test class would look like:
@isTest
private class PostalCodeUtilsTest {
	
	@isTest static void test_getPCCountry() {
		// Implement test code
		Test.startTest();

		// test null postalcode
		String returnValue = PostalCodeUtils.getPCCountry(null);
		System.assertEquals('United States', returnValue);
		//test postcode length 1 numeric
		returnValue = PostalCodeUtils.getPCCountry('1');
		System.assertEquals('United States', returnValue);
		//test postcode length 2 
		returnValue = PostalCodeUtils.getPCCountry('12');
		System.assertEquals('United States', returnValue);
		//test non-starting numeric
		returnValue = PostalCodeUtils.getPCCountry('P0L');
		System.assertEquals('Canada', returnValue);
		//test expected valid postal code
		returnValue = PostalCodeUtils.getPCCountry('10001');
		System.assertEquals('United States', returnValue);
		
		Test.stopTest();
	}
	
	@isTest static void test_getPCID() {
		// Implement test code 
		Test.startTest();

		//test both null
		String returnValue = PostalCodeUtils.getPCID(null,null);
		System.assertEquals('', returnValue);
		//test postcode null
		returnValue = PostalCodeUtils.getPCID(null,'ASIA');
		System.assertEquals('', returnValue);
		//test country null
		returnValue = PostalCodeUtils.getPCID('10001',null);
		System.assertEquals('10001', returnValue);
		
		//test Canada country
		returnValue = PostalCodeUtils.getPCID('P0L','Canada');
		System.assertEquals('P0L', returnValue);
		//test Canada country lowercasae postcode
		returnValue = PostalCodeUtils.getPCID('p0l','Canada');
		System.assertEquals('P0L', returnValue);
		//test valid values        
		returnValue = PostalCodeUtils.getPCID('10001','United States');
		System.assertEquals('10001', returnValue);
		//test short postcode        
		returnValue = PostalCodeUtils.getPCID('1234','United States');
		System.assertEquals('1234', returnValue);
		//test long postcode        
		returnValue = PostalCodeUtils.getPCID('123456','United States');
		System.assertEquals('12345', returnValue);

		Test.stopTest();

	}
	
}
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, etc


Hope that provides some insights.

Regards
Andrew G

All Answers

Jolly_BirdiJolly_Birdi
Hello Neo,

Please check the below Code:
 
@isTest
private class PostalCodeUtilss {
	
	@isTest static void test_method_one() {
		Test.startTest();
        String returnValue = PostalCodeUtils.getPCCountry(null);
        System.assertEquals('United States', returnValue);

        returnValue = PostalCodeUtils.getPCCountry('P0L');
        System.assertEquals('Canada', returnValue);

        returnValue = PostalCodeUtils.getPCCountry('10001');
        System.assertEquals('United States', returnValue);

        returnValue = PostalCodeUtils.getPCID(null,null);
        System.assertEquals('', returnValue);
        
        returnValue = PostalCodeUtils.getPCID('P0L','Canada');
        System.assertEquals('P0L', returnValue);
        
        returnValue = PostalCodeUtils.getPCID('10001','United States');
        System.assertEquals('10001', returnValue);
        
        Test.stopTest();
	}
	
	
}



Please mark this as the best answer if you find it positive.

Thanks
Jolly Birdi.
Andrew GAndrew G

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. 

I 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.
 

if (postalCode == null ||  postalCode.length() <= 1){
So my test class would look like:
@isTest
private class PostalCodeUtilsTest {
	
	@isTest static void test_getPCCountry() {
		// Implement test code
		Test.startTest();

		// test null postalcode
		String returnValue = PostalCodeUtils.getPCCountry(null);
		System.assertEquals('United States', returnValue);
		//test postcode length 1 numeric
		returnValue = PostalCodeUtils.getPCCountry('1');
		System.assertEquals('United States', returnValue);
		//test postcode length 2 
		returnValue = PostalCodeUtils.getPCCountry('12');
		System.assertEquals('United States', returnValue);
		//test non-starting numeric
		returnValue = PostalCodeUtils.getPCCountry('P0L');
		System.assertEquals('Canada', returnValue);
		//test expected valid postal code
		returnValue = PostalCodeUtils.getPCCountry('10001');
		System.assertEquals('United States', returnValue);
		
		Test.stopTest();
	}
	
	@isTest static void test_getPCID() {
		// Implement test code 
		Test.startTest();

		//test both null
		String returnValue = PostalCodeUtils.getPCID(null,null);
		System.assertEquals('', returnValue);
		//test postcode null
		returnValue = PostalCodeUtils.getPCID(null,'ASIA');
		System.assertEquals('', returnValue);
		//test country null
		returnValue = PostalCodeUtils.getPCID('10001',null);
		System.assertEquals('10001', returnValue);
		
		//test Canada country
		returnValue = PostalCodeUtils.getPCID('P0L','Canada');
		System.assertEquals('P0L', returnValue);
		//test Canada country lowercasae postcode
		returnValue = PostalCodeUtils.getPCID('p0l','Canada');
		System.assertEquals('P0L', returnValue);
		//test valid values        
		returnValue = PostalCodeUtils.getPCID('10001','United States');
		System.assertEquals('10001', returnValue);
		//test short postcode        
		returnValue = PostalCodeUtils.getPCID('1234','United States');
		System.assertEquals('1234', returnValue);
		//test long postcode        
		returnValue = PostalCodeUtils.getPCID('123456','United States');
		System.assertEquals('12345', returnValue);

		Test.stopTest();

	}
	
}
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, etc


Hope that provides some insights.

Regards
Andrew G
This was selected as the best answer
HNT_NeoHNT_Neo
Thank you both! I tested both versions and both worked, however, opted in covering more gaps in the code. I will, however, use what was provided to learn more about getting my Apex test class' inline.