Apex Testing: SmartFactory and Jenkins

When the evangelist team hits the road and talks to developer, we love talking about new features (like Apex REST and JavaScript Remoting – see, we can’t help ourselves…) – but I’ve consistently heard from user groups that the conversation about Apex principals and basics is every bit as important.  In that spirit, I went in search of tools for the Apex developer to make code testing more robust.  We’ll continue this search, but here’s two powerful, easy and free tools to help.

First is SmartFactory, written by Matthew Botos ( @BotosCloud as he is known on twitter ) of Maven Consulting for their 2011 Hackathon, which it won.  SmartFactory helps resolve one of the more time consuming aspects of creating proper unit tests: generating test data.  SmartFactory allows developers to use code that looks like this:

Account account = (Account)SmartFactory.createSObject('Account');

Instead of code that looks like this:

Account account = new Account(Name = 'Test Account123', RequiredField = '1234', OtherRequiredField = 'blah', YetAnotherRequiredField = 'foobar');

And so on.  Not only does this make writing unit tests easier and faster, it also should assist developers in making that classic mistake of unit testing where they simply pull an existing record in the system, a la:

Account account = [SELECT ID, Name, Phone from Account where ID =: {SOMEIDTHATWILLNOTEXISTONPRODUCTION} LIMIT 1];

Which, as many a team lead has probably found on a late Friday deploy – will almost assuredly fail when that test environment ID has no meaning on the instance on to which you are trying to deploy.

SmartFactory does require some manual setup prior to usage, in particular the generation of a class to handle the specific describe information for you organization.  It is also good to note that as the result of a one day hackathon, it may not be the most robust or heavily tested piece of software on the planet, but for creating test data it is surely a better starting point than simply writing the data from scratch.

Next up is Jenkins, which was brought to my attention via James Hatton, a.k.a @jameshatton on twitter – who will be presenting a session on continuous integration at Dreamforce … which even though it conflicts directly with a session I’ll be presenting on about jQuery, I still highly recommend.  Jenkins is a Java based continuous integration suite, branched off of Hudson, which is freely available and yet quite feature rich.  There isn’t enough room on a blog post to go into all the details, but I will illustrate that in an afternoon I had Jenkins pulling my HTML5 project out Github, attempting to deploy it to a test environment, and then reporting on the success every five minutes after the hour (if there were any changes in Github).

Jenkins can operate directly with the Force.com Migration Tool, which can be downloaded from any existing account and used with Ant for automated deployment.  Jenkins has an extremely convenient web interface for configuring these options and loading new modules (like the one for Github):

Continuous integration may not be necessary for every project, but if you have multiple developers working against the same sandbox, it can be a life saver.  Potentially each developer could be working off of different developer or sandbox edition accounts, committing to source control (and Jenkins has plugins for nearly every one out there), and then that source could be deployed and tested against a sandbox on a constant basis to keep the team ahead of any deployment problems prior to going to production.

If you’ve used either of these tools, please jump on the comments and add any details you might have with their usage.  Since SmartFactory is on Github, if you’ve got updates for Matthew – fork away.  I’ve got a quick and simple Visualforce page for rebuilding the SmartFactory helper class I still need to send over.  And of course, if you’ve got other best practice and testing needs … Dreamforce will have a wealth of information for developers, not just on CI but other development practices as well.

Published
July 21, 2011
Topics:

Leave your comments...

Apex Testing: SmartFactory and Jenkins