A Comprehensive Blog about Apex Testing

What is Apex?

Apex is a robust OOPs (Object Oriented Programming) language which is developed by Salesforce.com and used to develop Salesforce platforms. Developers can execute the flow and transaction control statements on the Salesforce servers in integration with calls to APIs.

Apex enables developers to add business logic to system events, like button clicks, by using syntax which is easy to understand for people with prior knowledge of Java. The Apex application programing interface allows developers to use data present on Salesforce.com. It also permits developers to access the backend databases and client-server interfaces of Salesforce.com and create Software as a Service (SaaS) applications.

The applications created using Apex are hosted on the Salesforce.com servers and do not require any installations on the user’s PC.

Why do you need to test Apex?

Like any other language and software development process, testing plays an important part in Apex application development as well. It is very essential to test software to ensure that it executes successfully and generates the expected output. This helps you deliver the best software to your customer’s increases your credibility.

Apex applications can be tested in 2 ways: first is by testing the user interface, the second is by testing its bulk functionality. You need to keep testing the Apex application after adding new functionality or upgrading the features of the application to ensure that no regressions are generated after the upgradation.

You must ensure these things before you deploy your code for the Salesforce AppExchange server:

  • 75% of your Apex code must be covered by unit testing and the tests must be executed successfully
  • The tests you perform must cover all the triggers
  • The classes and triggers used in your Apex code must compile successfully

Remember the following things:

  • All the unit tests in your organization namespace are executed by default when you deploy the Apex code
  • The test methods and test classes are not a part of the Apex code
  • You need to ensure that all the use cases of your application are covered when you perform unit testing. This way you will be able to test 75% of the Apex code efficiently.
  • Salesforce makes sure to run all Apex tests before any upgrades on behalf of the developer through a process called Apex Hammer. It does so in order to make verify that the functionality or the results do not get altered due to any upgradations. During the Apex Hammer process the issues found are fixed immediately and perfected before the upgrade id released.

What to test in Apex Testing?

There are few factors that you need to test your Apex code for:

  • Single action

You need to test and verify that every single record generates the expected output which is correct. This can be done by performing unit tests to check specific actions.

  • Bulk Functionality

Another thing you need to test during Apex testing is the bulk functionality of the code. Bulk actions may include trigger, class or an extension.

  • Positive behavior tests

Positive behavior tests need to be performed in order to ensure that the code generate expected output corresponding to very expected input. In this test the input values are kept within some limits and the code is tested for known values to check for expected behavior.

  • Negative behavior tests

Every application has a certain limitation to the inputs that it can take. In negative behavior tests the Apex code is tested for input values which are out of the limits to check the error messages that will be displayed in such cases.

  • Restricted test

You also need to test the code under restricted environment to check how it will perform when a user with limited access to application resources uses the application. The restrictions can include a limited access to objects or methods.

alt

Apex Testing

Apex testing process is quite similar to any other testing methods for web based applications. Testes need to have clear knowledge of the system under test so that they can focus on testing the core functionality of the application in detail by using different techniques instead of sticking to the features offered by Salesforce.

The apex code can only be written in a Sandbox environment and it needs to be deployed into a production environment in order to be tested. All the tests in Apex testing are performed in a production.

Unit Testing in Apex

Unit testing is performed on every single action to make sure that each unit of code performs efficiently and produces expected results. The most astounding feature of the Apex testing framework is that it will enable you to create and execute tests on all the classes as well as triggers on the Lightning platform. This confirms that the code is of high quality and meets customer requirement and specifications.

Benefits of Apex unit testing:

  • It ensures that 75% of the apex code is tested and executed
  • It verifies that all the classes and triggers work as expected and produce the desired output
  • Allows the creation of quality applications that meet all the customer expectations
  • Unit tests help in meeting code coverage requirements for deploying the code to production

The apex unit testing you need to develop separate test classes for each test case.

What are test classes?

In Apex unit testing, code coverage is performed with the help of test classes. The test classes are sections or units of code that test each unit for its correct and efficient functioning.

This is how you can create test classes:

1. Create the data

You first need to create the data for the test class. You need to provide this test class access to the organization data as it cannot access this data by default. If you set @isTest(seeAllData= True) the test class will be able to access the organization data.

2. Use @is Test

You need to the @isTest annotation to specify that the test class will not be considered against the total code limit of the organization.

3. the methods with ‘testMethod’ keyword

The test methods in apex unit testing do not take arguments, commit data to the database, do not send any emails and are declared with the testMethod keyword. They can also be declared with the isTest annotation in the method definition.

4. Starting and stopping the tests

The test classes can be controlled by using the Test.startTest() and Test.stopTest() classes. These classes contain the event or action which can invoke test classes and stop them as well.

5. .assert()

This method is used to compare the expected output with the actual output that is produced.

Challenges faced while performing Apex testing

Creating apex applications is not a simple task and testing the apex code is even more complicated. Here are some of the challenges that you may face while performing Apex testing:

  • Through apex programming advanced applications are created for Salesforce or server cloud console, and testing them is not an easy task.
  • For Lightning framework user interface all the classic tests need to be recreated.
  • As mentioned earlier, there are two ways of testing Apex code: GUI test and bulk functionality test. The GUI test does not work when the code is shifted to a test environment.
  • The standard functionalities cannot be removed even if they are not in use.
  • The automated test should work in all the test environment
  • Creating field locators for the Salesforce screen can prove to be a difficult task as the field IDs differ from organization to organization.

Best Practices for Apex testing

  • The best way to conduct apex test is by using real user profiles. This way you can find all the faults from the user’s perspective
  • You should prepare the test data to verify the reports functionality
  • You should test the apex code using all the testing methods including: unit tests, GUI test, regression testing, system testing, function testing and integration testing.
  • You should pay special attention to the visual force pages which are dynamic in nature to make sure that all the elements are able to load asynchronously
  • You need to perform automation testing by using tools like Selenium and HP unified functional testing
  • You must determine the limitations to the input in order to perform positive and negative testing
  • If the code includes conditions and ternary operations you must test each possibility and condition separately to ensure that the application deliver the most efficient output in every situation
  • You must ensure that apart from the apex code being error free it should also not generate any exception.
  • Always compare the actual output with the expected output by using the System.assert method
  • You must always perform the bulk functionality test by using at least 20 records in the test.
  • Create the required test data for your test classes
  • Always test all the classes in you apex code separately. It is a bad practice to test the entire application in a single test as detecting errors becomes very difficult once the system is complete.

This was a guide to Apex testing. You need to have knowledge about the Apex language in order to be able to test apex code efficiently and resolve the errors that are encountered.