FAQ and Best Practices of Test Classes in Apex

Here I am going to share few answers related to Test Classes, which is being asked many times to me by novice Programmers. Please feel free to post Comments if your question is not answered here. I will try my best to add those Questions in this article. If you want to learn Test Class from very basic, you can check this article also.

  1. Best Practices for Test Classes
  2. Use of SmartFactory to auto generate test data
  3. Test method of Controller Extension for StandardController
  4. Test method of Controller Extension for StandardSetController
  5. Why getSelected() method of StandrdSetController is not working
  6. sample Template of class which generates Dummy Data
  7. checking Error Page Messages in Test Classes
  8. Setting Visualforce Page Parameter

What are some Best Practices while writing Test Classes ?

Well, It differs from person to person, as every programmer has its own style of writing code. However I will list few of mine.

  • Very important and first, “Test Coverage Target Should not be limited to 75%”. It is not about coverage, It is about testing complete functionality. It will be always better if your code fails during test execution, It will be less devastating than failing functionality after product release.
  • If possible Don’t use seeAllData=true, Create your Own Test Data.
  • Always use @testSetup method dedicated to create test records for that class. This is newly added annotation and very powerful. Any record created in this method will be available to all test methods of that class. Using @testSetup method will improve test execution performance by creating test records only once for that class. If you are not using this, means test record is created in each TestMethod which will be very slow. Read this Salesforce documentation for more information.
  • Create Different Class which will create Dummy Data for testing, and use it everywhere (You have to be very careful, as sometimes it may slow down test class execution by creating unnecessary data which does not require by every test methods. So few developer prefer test data creation per Test class)
  • If your Object’s Schema is not changing frequently, you can create CSV file of records and load in static resource. This file will act as Test data for your Test Classes.
  • Use As much as Assertions like System.AssertEquals or System.AssertNotEquals
  • Use Test.startTest() to reset Governor limits in Test methods
  • If you are doing any Asynchronous operation in code, then don’t forget to call Test.stopTest() to make sure that operation is completed.
  • Use System.runAs() method to enforce OWD and Profile related testings. This is very important from Security point of View.
  • Always try to pass null values in every methods. This is the area where most of program fails, unknowingly.
  • Always test Batch Capabilities of your code by passing 20 to 100 records.
  • Use Test.isRunningTest() in your code to identify that context of class is Test or not. You can use this condition with OR (||) to allow test classes to enter inside code bock. It is very handy while testing for webservices, we can generate fake response easily.
  • @TestVisible annotation can be used to access private members and methods inside Test Class. Now we don’t need to compromise with access specifiers for sake of code coverage.
  • End your test class with “_Test”. So that in Apex Class list view, Main class and Test class will come together, resulting easy navigation and time saver.


Use of SmartFactory to auto generate test data

This is very common pattern where we create test data in some utility method ans reuse it across test methods. However change in requirement is inevitable and many validation rules and mandatory fields gets introduced in application and test methods starts breaking. We can resolve this issue by using unmanaged package available on github which creates Test data and hierarchy automatically using dynamic Apex and make sure all required fields are populated.

Just use SmartFactory in your tests to create objects:

Account account = (Account)SmartFactory.createSObject('Account');

To cascade and create objects for lookup and master-detail relationships:

Contact contact = (Contact)SmartFactory.createSObject('Contact', true);

The same syntax is used for custom objects:

Custom_Object__c customObject = (Custom_Object__c)SmartFactory.createSObject('Custom_Object__c');

How to write Test method of Controller Extension for StandardController ?

Example :

//Lets Assume we are writing Controller Extension for Account
Account acct = [SELECT ID FROM Account LIMIT 1];

//Start Test Context, It will reset all Governor limits
Test.startTest();

//Inform Test Class to set current page as your Page where Extension is used
Test.setCurrentPage(Page.YOUR_PAGE);

//Instantiate object of "ApexPages.StandardController" by passing object
ApexPages.StandardController stdController = new ApexPages.StandardController(acct);

//Now, create Object of your Controller extension by passing object of standardController
YOUR_Extension ext = new YOUR_Extension(stdController);

//Here you can test all public methods defined on Extension "ext"
//..... your code

//Finish Test
Test.stopTest();

How to write Test method of Controller Extension for StandardSetController ?

In Same way, with few modification :

//Lets Assume we are writing Controller extension to use on List View of Account
List <Account> acctList = [SELECT ID FROM Account];

//Start Test Context, It will reset all Governor limits
Test.startTest();

//Inform Test Class to set current page as your Page where Extension is used
Test.setCurrentPage(Page.YOUR_PAGE);

//Instantiate object of "ApexPages.StandardSetController"by passing array of records
ApexPages.StandardSetController stdSetController = new ApexPages.StandardSetController(acctList);

//Now, create Object of your Controller extension by passing object of standardSetController
YOUR_Extension ext = new YOUR_Extension(stdSetController);

//Here you can test all public methods defined on Extension "ext"
//..... your code

//Finish Test
Test.stopTest();

Why getSelected() method of StandrdSetController is not returning anything?

You might be in situation that getSelected() method is not returning anything in your test method  inspite of proper coding. In Actual scenario we select records in ListView and after clicking on custom button, StandardSetController’s getSelected() method returns selected record in Controller Extension. So, to Select records programmatically  in Test method we have to use method “setSelected()” as shown in below code.

//Lets Assume we are writing Controller extension to use on List View of Account
List <acctList> = [SELECT ID FROM Account];

//Start Test Context, It will reset all Governor limits
Test.startTest();

//Inform Test Class to set current page as your Page where Extension is used
Test.setCurrentPage(Page.YOUR_PAGE);

//Instantiate object of "ApexPages.StandardSetController" by passing array of records
ApexPages.StandardSetController stdSetController = new ApexPages. StandardSetController(acctList);

//Here, you are selecting records programmatically, which will be available to method "getSelected()"
stdSetController.setSelected(acctList);

//Now, create Object of your Controller extension by passing object of standardSetController
YOUR_Extension ext = new YOUR_Extension(stdSetController);

//Here you can test all public methods defined on Extension "ext"
//..... your code

//Finish Test
Test.stopTest();

Any sample Template for method or class which generates Dummy Data?

You can create methods something like below to generate TestRecords. It will change on requirement however will give some idea.

/**
*	Utility class used for generating Dummy Data for Test Methods
**/
public class DummyData
{
	/**
	*	Description : This method will generate List of Account with Dummy Data
	*
	*	Parameters :
	*	@totalRecords : How many Records you want to generate ?
	*	@withIds : Do you want returned records with generateId? If null then false
	**/
	public static List<Account> getAccounts(Integer totalRecords, Boolean withIds)
	{
		List<Account> retList = new List<Account>();
		if(withIds == null)
			withIds = false;

		for(Integer i=0;i<totalRecords;i++)
		{
			Account a = new Account(Name = constructTestString(20));
			retList.add(a);
		}
		if(withIds)
			insert retList;

		return retList;
	}

	/**
	*	This method is used to generate Random String of supplied length
	*/
	public static String constructTestString(Integer length) {
		Blob blobKey = crypto.generateAesKey(128);
		String key = EncodingUtil.convertToHex(blobKey);
		return key.substring(0,length);
	}
}

How to check Error Page Messages in Test Classes to ensure that messages are displayed as expected on Visualforce Page ?

Assume that you have one Controller class which adds error message to Visualforce page.

public class DemoController
{
    //... some code

    public PageReference somemethod()
    {
        //Check for some validation. like User is Authorized or not ?
        if(!validate())
        {
             //Means User is not Authorized for this operation, Add error on Visualforce page
             Apexpages.addMessage( new ApexPages.Message (ApexPages.Severity.ERROR, 'User is not Authorized to perform this Operation'));
             return null;
        }
        return Page.SomePage;
    }
}

Now, lets say you want to check whether error message is added on visualforce page or not in Test Class.

@isTest
public class DemoController_Test
{
    public static testMethod void someMethod_Test()
    {
        Test.StartTest();

        //Set Context for Current page in Test Method
        Test.setCurrentPage(Page.yourpage);

        //... some code Here, which will produce error in Apex:PageMessages tag
        List<Apexpages.Message> msgs = ApexPages.getMessages();

        boolean isErrorMessage = false;

        for(Apexpages.Message msg : msgs){
            if (msg.getDetail().contains('User is not Authorized to perform this Operation') )
                isErrorMessage  = true;
        }
        //Assert that the Page Message was Properly Displayed
        system.assert(isErrorMessage );

    }
}

If you are looking on how to add “Page error message” in Visualforce through Apex, You can see this article.


How to set Page Parameters for Visualforce page in Test Classes ?

First, we need to set Current page in Test method and use “ApexPages” class. Example of code snippet shown below :

@isTest
public class Your_TestClass
{
    public static testMethod void yourmethodName()
    {
        //... your initialization code here

        //Set current Page Context here, consider page name "Demo"
        Test.setCurrentPage(Test.Demo);

        //Now set Parameters for page "Demo"
        ApexPages.currentPage( ).getParameters( ).put( 'parameterName' , 'param Value');

        //... Your remaining logic here
    }
}

Posted

in

by


Related Posts

Comments

12 responses to “FAQ and Best Practices of Test Classes in Apex”

  1. AJAY KUMAR SINGH Avatar
    AJAY KUMAR SINGH

    Great Thanks

  2. Harshit Pandey Avatar

    Cool wrap up – but here are some guidelines that,I would like to add based on my experience

    1. if (Test.isRunningTest()) {} whenever required to easily bifurcate difference between logic that is desired to be run only on or not on test,

    Example : if you can’t make API callout from testclass (which you can’t) then you need to fake it, like here I am calling a third party company API only if not running test else faking response

    var response = “getafakeresponse” //which you do through API call

    if(Test.IsrunningTest()) { HttpResponse = response; …… and logic goes there }

    2. TestVisible Annotation : With this annotation, you don’t have to change the access modifiers of your methods and member variables to public if you want to access them in a test method. For example, if a private member variable isn’t supposed to be exposed to external classes but it should be accessible by a test method, you can add the TestVisible annotation to the variable definition.

    3. Data Generation : for testMethods

    Like wise you said, create a different class for dumping data to your testclass like say DataUtil class that creates objects , then use a wrapper on top of it. For each project you can make project specific helper class like if you working on New Lead Process process design then design a new helper like testNewLeadProcessController class which in turn call you ‘DataUtil class’ this makes it easier to maintain project specific testclasses

    1. DataUtil —- called by — > TestNewLeadProcessController —–is test class for NewLeadProcessController

    2. Keep naming convention starting with annotation ‘test’ to ease the sorting

    3. DataUtil – where you create object – try not to insert data instead just return, that that allow you freedom to manipulate the sObject before you commit for first time and reduce one DML operation

    Like

    DataUtil {

    //do not insert – return and insert when you implement it
    public Contact createContact ( Contact c = new Contact (firstName=’Harshit’; lastName=Pandey; return c; )

    }

    MyTestClass {

    //make manipulation before insert, rather then inserting in datautil and updating here
    DataUtil data = new DataUtil();
    Contact c = data.createContact();
    c.phone=’1123;
    insert c;

    }
    }

    Few more but seems super long comment 🙂

    1. JitendraZaa Avatar
      JitendraZaa

      Thanks Harshit for Listing. It helped, I have updated post with some of your points.

  3. Guest Avatar
    Guest

    It would awesome if you explain a little more on ‘ Error Page Messages in Test Classes’ when and how is this used . Why can’t we de-couple it to a separate Error handler class, if my understanding is purely wrong, then please correct me

  4. Harshit Pandey Avatar

    It would awesome if you explain a little more on ‘ Error Page Messages in Test Classes’ when and how is this used . Why can’t we de-couple it to a separate Error handler class, if my understanding is purely wrong, then please correct me

    1. JitendraZaa Avatar
      JitendraZaa

      I have updated Question with more detail.. Please let me know if it helps..

  5. Dinesh Singla Avatar
    Dinesh Singla

    hi jitendra
    i have addd a custom look up of Campaign on lead page but i am not able to add custom look filter on that look up.can u plz tell me any way to add look up filter

  6. Aamir Avatar
    Aamir

    Hi Jitendra
    I have an opportunity it has 1 million record i want to create test class for this object
    Can u plz tell me how to write test class for this object

  7. Dhaval Shah Avatar
    Dhaval Shah

    Hi Jitendra,

    I am a new bie and I want to get started with APex Programming, can you please guide me?

  8. Nayab Rasool Avatar
    Nayab Rasool

    Hi Jithendra,

    Always try to pass null values in every methods. This is the area where most of program fails, WHY?

    1. Jitendra Zaa Avatar

      Hi Nayab, this is negative testing strategy. Try to check how code behaves in case of invalid inputs. Instead of passing you may also pass any invalid parameter to methods.

  9. Dave Morris Avatar
    Dave Morris

    Fantastic, thank you for this!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Discover more from Jitendra Zaa

Subscribe now to keep reading and get access to the full archive.

Continue Reading