What is unit test class in Apex?

A Unit Test is a class method that is used to verify if a certain code is working or not. This facilitates the creation of code that is error-free and more robust.

What there is to knowe it

Test methods need to be defined in classes that are annotated isTest (i.e. test classes). They are also flagged with the keyword “testMethod.”

By using the isTest notation you are declaring that the following code is a test class. That also means it will not be counted in the code limit for your organization. Note that there is a 3 MB limit for the Apex code in your organization.

When you flagged the class with the testMethod keyword, it means that the unit test will commit no data. It will also not take arguments and it will also not send any emails.

Considerations

  • You can no longer include test methods within non-test classes (this started with Salesforce API 28.0).
  • You are not allowed to use them to test Web service callouts. Test methods can use mock callouts instead.
  • Upon the completion of a test, you do not need to delete any test data since test methods do not really commit any data that is created anyway.
  • When test methods are used to modify an associated record, that record’s tracked changes will not be available in Chatter feeds.
  • You will get an error if you insert duplicate Object records that have unique constraints.
  • The value of a variable will be changed in a test method even if it contains a static member variable.

Note that the default access level for unit test classes in Apex is set as private. However, you can define methods and classes as either public or private and it does not really matter. The testing framework will always be able to find them and eventually execute them regardless of access level.

Leave a Reply

Close Menu