How Do You Run a Unit Test in Apex?
To facilitate the development of robust, error-free code, Apex supports the creation and execution of unit tests. Unit tests are class methods that verify whether a particular piece of code is working properly. Unit test methods take no arguments, commit no data to the database, and send no emails. Such methods are flagged with the @isTest annotation in the method definition. Unit test methods must be defined in test classes, that is, classes annotated with @isTest.
Here is the same test class as in the previous example but it defines the test method with the (now deprecated) testMethod keyword instead.
Use the @isTest annotation to define classes and methods that only contain code used for testing your application. The @isTest annotation can take multiple modifiers within parentheses and separated by blanks.
This example of a test class contains two test methods.
Classes and methods defined as @isTest can be either private or public. The access level of test class methods doesn’t matter. You need not add an access modifier when defining a test class or test method. 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 test method or code invoked by a test method; non-test requests can’t invoke it.
This example shows a class to be tested and its corresponding test class. It contains two methods and a constructor.
This example contains the corresponding test class with four test methods. Each method in the previous class is called. Although there is sufficient test coverage, the test methods in the test class perform extra testing to verify boundary conditions.