Activity Forums Salesforce® Discussions Is it important to include @isTest on Testing Method in Salesforce? Explain?

  • Anurag

    Member
    July 19, 2018 at 3:12 pm

    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.

     

  • shariq

    Member
    September 16, 2018 at 3:35 pm

    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.

  • Parul

    Member
    September 21, 2018 at 9:52 pm

    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.

Popular Salesforce Blogs

Popular Salesforce Videos