The Apex testing framework makes sure that developers can write and execute tests for all the Apex Classes and triggers in the Force.com platform. In the testing framework, the code is tested and the testing code is coded in the sandbox environment and then deployed to production Org. 

Apex Test Class Best Practices

1. Use @isTest at the Top for all the test classes.

2. Always put assert statements for negative and positive tests.

3. Utilize the @testSetup method to insert the test data into the Test class that will flow all over the test class.

4. Always make use of Test.startTest() and Test.stopTest() doing this it will increase the governor limit of the salesforce. We also use this to increase the governor’s limit.

5. Use System.runAs() method to test the functionality in user Context.

6. Do not put (seeAllData = true) in test class otherwise, use it for exceptional cases.

7. Avoid Using Hard Coding Ids anywhere in test Class or any apex class.

8. Ensure that each class has a minimum of 75% coverage and also the main functionality has been covered. If possible increase code coverage up to 95%.

9.  All class methods must be tested for at least 200 records and keep the real scenarios in mind.

10. Only one Test.startTest() and Test.stopTest() statement can be in a method, and no of  Test.startTest() and Test.stopTest() statement in any test class depend upon the test methods.

Test Class Example

@isTest

public class setOpportunityOwner 

{

    public TestMethod static void setOpportunityOwner_Method()

    {

        Opportunity opp = New Opportunity();

        opp.name = ‘Hello’;

        opp.stageName = ‘Prospecting’;

        opp.CloseDate = Date.today();

        insert opp;

        Task tk = New Task();

        tk.WhatId = opp.Id;

        tk.Subject = ‘Other’;

        tk.status = ‘Not Started’;

        tk.description = ‘New  Work’;

        Test.startTest();

        Database.SaveResult str = database.insert(tk , False);

        System.assertEquals(True, str.isSuccess());

        Test.stopTest();

    }

}

Advantages of Apex Test Classes

  • You can use Test.isRunningTest() within your code to recognize that the context of the class is Test or not. 
  • You can use this condition with OR (||) operator to enable test classes to highlight inside the Code block.
  • Utilize @TestVisible annotation to identify private members and methods inside Test Class.

Salesforce Tasks in Apex

A task in Salesforce is defined as an assigned action that is required to be done by the user to whom the task is assigned. Tasks can be related to leads, campaigns, contacts, and contracts.

Difference between WhatID and WhoID in Task

WhatID in Salesforce – It represents object type things. It can easily relate to AccountID or an Opportunity ID. The WhatId refers to nonhuman objects like accounts, campaigns, opportunities, or custom objects. WhatId occurs in several different forms that means WhatId equals to the ID of a related object. The label for WhatID is Related To.

WhoID in Task – WhoID represents people things. It can easily relate to Lead ID or a Contact ID. It refers to human-like a lead and a contact. WhoID equals to a contact’s ID or a lead’s ID. The label for WhoID is Name.

Conclusion

The Apex testing framework is an innovative tool enables you to write and execute your Apex Code. Test class code is developed in a Sandbox environment and deployed to a production org. Apex unit tests make sure that your Apex classes and triggers perform as expected. By following the above practices, you can easily write Apex triggers and classes in Salesforce and hence make your system both manageable and Scalable.

In order to learn more about Apex Triggers, check our latest webinar video on 

Change Data Capture & Asynchronous Apex Triggers presented by Sachin Arora, the Scrum Master and Principal Solutions Architect at Cloud Analogy

Pin It on Pinterest