Apex provides a testing framework that allows to write unit & run tests, check test results, and have code coverage results. Altogether, Testing is the key to successful long-term development and is a critical component of the development process. Apex code basically written in a sandbox environment or a developer organization, not in production.
Why Test Apex?
There are two ways of testing an application. One is through the Salesforce user interface, important, but merely testing through the user interface will not catch all of the use cases for your application. The other way is to test for bulk functionality following code using by a Visualforce standard set controller. Certainly, Apex code can be shipped off a production association from a sandbox. Additionally, app developers can distribute Apex code to clients from their developer organizations by transferring bundles to the Lightning Platform AppExchange. The unit tests are similarly essential for sending and disseminating APEX.
Salesforce developers tests for the following:
To check whether an individual record generates the appropriate or expected result.
Any Apex code like a class or a trigger or extension can call for 1 to 200 records. You should test individual or Bulk records.
To check generally the assumed behavior happens in every assumed mutation.
There are likely limits to your applications, such as not being able to add a future date, not being able to specify a negative amount, and so on. You must test for the negative case and verify that the error messages are correctly produced as well as for the positive, within the limits cases.
To test whether the user implements the code or gets error messages.
APEX Unit tests:
To ease the development of strong and flawless code, Apex endorses the execution and creation of unit tests. Unit tests also considered as class methods that test whether a specific part of code is working accurately. Whereas Methods of Unit tests will have no arguments, will not save data to the database.
Apex test data is temporary and also not saved in the database. After executing the test method, the inserted data is not saved in the database. The temporary behavior eases data management.
Running unit tests
To verify the utility of the apex code, We will run the apex test methods in the developer console.
- Few or total all test methods in a particular class.
- Few or full methods in a group of classes.
- An inbuilt collection of classes called a test suite.
- Complete unit tests in the organization.
We can run the unit tests through the following:
- Salesforce user interface
- Salesforce expansions through visual studio code.
- Lightning Platform Developer Console.
Running Unit tests through the Salesforce Interface:
1. In the Setup, type “Apex Test Execution” in the “Quick Find Box”, then choose
2. Press “select Tests”.
3. Choose the tests to run. The catalogue contains simply classes that comprise test methods:
- a test within an installed package, choose a namespace of a managed package from the fold-out list.
- a test that is present at the local level of your enterprise, choose [My Namespace] from the fold-out list.
- To choose a test, select [All Namespaces] from the fold-out list.
4. To withdraw from gathering code coverage data while running tests, choose “Skip Code Coverage”.
5. Press Run.