Tuesday, July 23, 2013

Writing Unit Tests...For everything in SFDC!

Unit tests have been covered on tons of different blogs by tons of way more qualified developers but I figured I'd write an entry which compiles all the best info I have found around the net.  I really like to reference my own blog as a resource master, so this will help a future me out, as well! :)

 First and foremost is the official Developer force page on writing good unit tests, which can be found HERE.  It talks about structure and what to do and not to do.

Second I would recommend going over the information found HERE, regarding the IsTest annotation.  This covers @isTest(SeeAllData=true) , which is a must have if you have to write code against things like the PriceBook2, which will return no rows during tests.

Next I would check out the usual suspects where good coding examples can be found.  Places like Jeff Douglas's blog.  He has a nice entry on writing Unit Tests f  or Visual Force Controller Extensions, which can be found HERE.  Another really good reference is Tquila Team, and they cover a bunch of cool topics on writing Unit Tests, HERE.

One other thing to be aware of is, test.isRunningTest() for your controller or trigger code.  Sometimes if you don't want a section of code to be hit by a test you can wrap it in an If statement testing this system test method.  The details about it can be found on the test method's page, HERE.  Along with a lot of other useful info regarding the test class and its methods.

I figure I should include some code in this posting so, I'll write a Unit Test for both a standard trigger  and a Visual Force page, which will instantiate its Custom Controller/Extension...I guess I'll put a snippet for both, since an extension has to have the Standard Object passed to it, as seen in JD's blog posting.  More later....