Activity › Forums › Salesforce® Discussions › Is it important to include @isTest on Testing Method in Salesforce? Explain?
Tagged: Annotation, Compiler, IsTest, Test Class, Test Method
-
Is it important to include @isTest on Testing Method in Salesforce? Explain?
Posted by Avnish Yadav on July 3, 2018 at 1:04 PMIs it important to include @isTest on Testing Method in Salesforce? Explain?
Parul replied 7 years, 7 months ago 4 Members · 3 Replies -
3 Replies
-
Hii Avnish,
Yes, it is important to denote a test class with a separate syntax so that the compiler can understand that its a class where we are going to perform a test. But, instead of using an annotation @isTest, we can also use ‘testmethod’ in a method name.
- [adinserter block='9']
-
Hi,
First what is @isTest annotation –
Use the @isTest annotation to define classes and methods that only contain code used for testing your application. The@isTest annotation on methods is equivalent to the testMethod keyword. The @isTest annotation can take multiple modifiers within parentheses and separated by blanks.
Classes and methods defined as @isTest can be either private or public. Classes defined as @isTest must be top-level classes.
@isTest
private class MyTestClass {// Methods for testing
@isTest static void test1() {
// Implement test code
}@isTest static void test2() {
// Implement test code
}}
Hope this helps.
-
Adding some points:
This class is defined using the @isTest annotation. Classes defined as such can only contain test methods. One advantage to creating a separate class for testing as opposed to adding test methods to an existing class is that classes defined with isTest don’t count against your organization limit of 3 MB for all Apex code.
The advantage to using separate test classes is that they do not count against the limit of the total amount of apex in your org (although this is a soft limit and thus not a overly compelling reason).@isTest
public class TestUtil {public static void createTestAccounts() {
// Create some test accounts
}public static void createTestContacts() {
// Create some test contacts
}}
Thanks
Log In to reply.