3

In apex documentation it is stated that "Classes and methods defined as isTest can be either private or public". But while writing code for it I found that it can be marked as global also. Why so ?
The following code compiles

@isTest
global class MyTestClass 
{
}

Thanks in advance !

2 Answers 2

3

I am not sure exactly where you are looking, but I found similar verbiage in the developer testing intro, and while they say that they can be either private or public, I think the point they are making is when you would pick one over the other, as these are the most common choices.

If we review the documentation around the class modifiers, we see that

All classes that contain methods defined with the webService keyword must be declared as global.

Salesforce example of a Global Test Class

2

You can use private, public, or global for classes annotated with @isTest. This allows you to share functions between classes:

private: No outside class can access the class, but you can run test methods on this class.

public: You can provide shared methods, such as TestUtil class, to provide standard functionality to all your tests. The most usual case is a suite of methods for creating accounts, contacts, etc in a standardized format for testing. If this is a managed package, these methods are not visible outside the package.

global: You can provide shared methods, such as a TestControl class, to provide standard functionality to all your tests and your clients' tests. The usual case is when you want to provide hooks for your clients to use during testing, such as disabling asynchronous (@future, Queueable) calls, which can cause problems in test methods, or even completely disabling triggers virtually for the purposes of testing. Sadly, I've never seen developers do this, which leads to many questions here and elsewhere about how to manage the situation. Outside of managed code, public and global are effectively the same, which is why public is normally the value you'd want to use over global. Global is more of interest to ISV developers.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .