Thursday, 9 June 2011

Testing Apex : Structure of Test Class for Apex Class

I saw a lot of questions on Test Methods on developer community asking how to write test class for apex class. So here I am writing a basic structure of a test class which tests an Apex Class
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();

        //call the method that you want to test using instance of class
        //assert your results using system.assert and system.asserEquals 
        // IF you have used any static variables


In above structure we have not talked about the advance problems like mixed dml exception and profile base testing. We leave it for other day :)



  1. Thanks so much for this post, mate! I've been looking for info that really clearly lays out the taxonomy of Apex Test Classes, and you nailed it. :)


    1. IEEE Final Year Project centers make amazing deep learning final year projects ideas for final year students Final Year Projects for CSE to training and develop their deep learning experience and talents.

      IEEE Final Year projects Project Centers in India are consistently sought after. Final Year Students Projects take a shot at them to improve their aptitudes, while specialists like the enjoyment in interfering with innovation.

      corporate training in chennai corporate training in chennai

      corporate training companies in india corporate training companies in india

      corporate training companies in chennai corporate training companies in chennai

      I have read your blog its very attractive and impressive. I like it your blog. Digital Marketing Company in Chennai

  2. Very nice post here and thanks for it .I always like and such a super contents of these post.
    Excellent and very cool idea and great content of different kinds of the valuable information's.
    Java training in Chennai
    Java training in Bangalore
    Java online training
    Java training in Pune

  3. This is an awesome post.Really very informative and creative contents about Java. These concept is a good way to enhance the knowledge.I like it and help me to development very well.Thank you for this brief explanation and very nice information.Well, got a good knowledge.
    Java training in chennai | Java training in annanagar | Java training in omr | Java training in porur | Java training in tambaram | Java training in velachery

  4. Good Post! , it was so good to read and useful to improve my knowledge as an updated one, keep blogging. After seeing your article I want to say that also a well-written article with some very good information which is very useful for the readers....thanks for sharing it and do share more posts like this.
    Tableau Online Training