Free Salesforce Developers Tutorials >

Chapter 11 - Apex Testing >

Apex Testing Basics

Apex Testing Basics

What You’ll Learn


tickWhat is Apex Testing?

tickPurpose of Apex Testing

S2 Labs

What is Apex Testing?

Apex testing is the process that helps you to create relevant test data in the test classes and run them in Salesforce. As a part of Apex testing, you need to test for:

  1. Positive behaviour: Test Apex code to verify that it works as per specification when appropriate input is provided to the code.
  2. Negative behaviour: Test the limitations of the system if an unexpected value or input is provided to the Apex code
  3. Restricted user: Check that a user whose access to certain objects or records is restricted is barred from accessing them

Understanding Testing in Apex

  1. Apex provides a testing framework that allows you to write unit tests, run your tests, check test results, and have code coverage results.
  2. Testing is the key to successful long-term development and is a critical component of the development process. It is strongly recommended that you use a test-driven development process, that is, test development which occurs at the same time as code development. 
  3. Testing is key to the success of your Application, particularly if your application is to be deployed to customers. If you validate that your application works as expected, and that there are no unexpected behaviours, your customers are going to trust you more.
  4. There are two ways of testing an application:
    • One is through the Salesforce user interface, important, but merely testing through the UI will not catch all the use cases for your application. [MANUAL]
    • The other way is to test for bulk functionality through your code if it’s invoked using SOAP API or by Visualforce standard set controller. [Automated]
  5. Before you can deploy your code or package it for the Force.com AppExchange, the following must be true:
  • At least 75% of your Apex Code must be covered by unit tests, and all of those tests must complete successfully.
        • Every trigger must have some test coverage.

        • All classes and triggers must compile successfully.

      Purpose of Apex Testing

        • To ensure that all the configuration you have built-in Salesforce and your Application works as per the specification.
        • To create a robust, long-lasting system that your customers can rely on.

      • To ensure there are no scenarios that are unhandled.

      • To ensure that there are no errors that you might have missed in the code.

      • To find any errors or unexpected behaviours in the code.

       

      Note:

      In Salesforce, if the Apex code is not properly tested, it can’t be deployed into the Production environment.

       

      What Are Apex Unit Tests & How To Write Them?

      Unit tests are class methods that verify whether a particular piece of code is working properly or not.

      Unit test methods take no arguments, commit no data to the database, send no emails, and are flagged with the testMethod keyword or the isTest annotation in the method definition. 

      Also, test methods must be defined in test classes, that is, classes annotated with isTest.

      Create different test methods to test different functionalities.

      In each test, method writes different test cases to test your code whether it is working properly with the different inputs or not.

      After API version 32 test method is used as @istest.

      For ex:

       @isTest
      private class MyClass {
          static void testMethod myTest {
              // code block 
          }
      }

      @isTest
      private class MyClass {
          @isTest
          static void myTest() {
              // code block 
          }
      }

      Use the isTest notation to define classes and methods that only contain code used for testing your application. The isTest annotation on methods is equivalent to the testMethod keyword.

      Note:

      Classes defined with the isTest annotation don’t count against your organization limit of 3 MB for all Apex code.

      This is an example of a test class that contains two test methods:

       @isTest
      private class MyTestClass {
          // Methods for testing 
          @isTest static void test1() {
              // Implement test code 
          }
          @isTest static void test2() {
              // Implement test code 
          }
      }➔

          • Classes and methods defined as isTest can be either private or public. The access level of the test methods doesn’t matter. This means you don’t need to add an access modifier when defining a test class or test methods. The default access level in Apex is private. The testing framework can always find the test methods and execute them, regardless of their access level.

          • Classes defined as isTest must be top-level classes and can’t be interfaces or enums.

          • Methods of a test class can only be called from a running test, that is, a test method or code invoked by a test method, and can’t be called by a non-test request.

        Characteristics Of Unit Test Methods

        Some characteristics of unit test methods are that:

            • are contained in separate classes which are called Test Classes specifically created to run tests.

            • do not commit any data to the database.

              • do not send any emails.

              • are always flagged with the keyword testMethod or @isTest annotation at the method definition level.

              • must always be defined in a test class. This test class should be annotated with @isTest annotation.

              • are always defined as static methods.

            salesforce-developer

          Download Study Material

          Get access to exclusive study material for Salesforce Certification and ace your exams!

          Download Now

          Our Salesforce Certification Courses

          Hey there! Glad you made it through our Salesforce Developer Training for beginners . But wait! We've got some high-in-demand Salesforce courses for you to take your Salesforce skills to the next level, making you a desired professional in the Salesforce job market.

          Post a Comment

          Your email address will not be published. Required fields are marked *