+ Start a Discussion
Gourav JainGourav Jain 

how to write test class

please sent me the structure of test class?

Navatar_DbSupNavatar_DbSup

Hi,

 

 Structure of Test Class for Apex Class:-

@isTest 

private class testApexClass{ 

 private static TestMethod void testClassMethod(){ 

     

        //Step 1 : Data Insertion 

        //Insert all the data required for your trigger according to your logic written in the trigger 

         

        //Try to give unique values in fields while creating test data, like Name = 'TestAccountName' etc 

         

        //If any configuration setting is also needed in your trigger insert that as well 

        //If you create any configuration setting then you will need to create a user also so that mixed dml exception do not occur 

         

        //If class has a constructor with parameter Standard Controller, that means VFP is using this class as extention and has a standard controller then please create a standard controller prior to instance of class 

 

        //Create instance of Class  

        MyClass cls = new MyClass(); 

 

        test.startTest(); 

         

        //call the method that you want to test using instance of class 

        cls.MethodToTest() 

          

        //assert your results using system.assert and system.asserEquals  

         

        test.stopTest(); 

        // IF you have used any static variables 

         

         

    } 

 

 

 

 

Testing Trigger : Structure of Test Class for Apex Trigger:-

 

@isTest 

private class testClass_Trigger{ 

 

    private static TestMethod void testTrigger(){ 

         

        //Step 1 : Data Insertion 

        //Insert all the data required for your trigger according to your logic written in the trigger 

         

        //Try to give unique values in fields while creating test data, like Name = 'TestAccountName' etc 

         

        //If any configuration setting is also needed in your trigger insert that as well 

        //If you create any configuration setting then you will need to create a user also so that mixed dml exception do not occur 

         

        test.startTest(); 

         

        //If there are any static variables which are used in the trigger please set there values 

         

        //Perform the dml action on which trigger gets fired , like insert, update ,delete , undelete 

         

        //assert your results using system.assert and system.asserEquals  

         

        test.stopTest(); 

        // IF you have used any static variables 

         

         

    } 

 

 

}

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved.