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
@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
        
        
    }


}

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 :)

Regards

25 comments:

  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. :)

    Cheers,
    Charlotte

    ReplyDelete
  2. 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

    ReplyDelete
  3. It s a very useful page. Thank you. c03d90788b04aa200bfd678a27cfcab6
    sirvan
    eyupsultan
    ahlat
    pamukkale
    cayirli
    cankaya
    kurucasile
    erenler
    kemer

    ReplyDelete
  4. It s a very useful page. Thank you. 422d3fb35a1c888c6c91e5d12b825b63
    kabaduz
    kozan
    aksoy
    doganyol
    saimbeyli
    bahcelievler
    besikduzu
    ciftlikkoy
    cat

    ReplyDelete
  5. Congratulations on your article, it was very helpful and successful. df461a5dd4d1bbe03a342ba1e44425d1
    sms onay
    website kurma
    website kurma

    ReplyDelete
  6. Thank you for your explanation, very good content. 86eb8cad0b9b0aee86a0f7d99a97ce41
    altın dedektörü

    ReplyDelete

Tweet