Written by Mathieu Bonin, Force.com Developer at Nubik
For the first Nubik Developer Expo, DevX, I was tasked with presenting a newly available tool to our team. That tool was Code-Robot. It sounded great on paper, but now that I’ve had the chance to use it on actual projects, I feel better equipped to give you a tour. If you’ve ever had to immense pleasure (ahem) of having to create unit tests, you’ll thank me.
What Is Code-Robot?
But before we get to you thanking me, it’s probably a good idea to talk about what Code-Robot even is. Well, in their own words it’s, “designed to take data from a Salesforce ORG and convert it into well-structured, deployable Apex test code.”
You can plug Code-Robot into a live production environment and target records for the objects you’re doing unit tests for. Code-Robot will look at all the lookups and relationships for those records, and generate code that will give you access to a copy of those records to put through your test code.
Why is that so cool?
It feels a little self-explanatory to anyone who has jumped in a massive client org before without a thorough knowledge of all the interconnected objects, processes, and workflows in place… but let’s say you’ve never had such a scenario…
On one hand, creating records in unit tests obviously requires a lot of manual work filling in required fields, but it also requires creating records for all the objects that are incidental to what you’re actually testing, since the tests records are submitted to the same triggers and workflows as in the real org. Often, just making sure your test records even work can eat up many hours of dev time. Not to mention it’s just unpleasant to analyse and build.
An image is worth a thousands words they say, I suppose an image of words might be cheating a bit, but it’ll have to do. Here’s the amount of code that Code-Robot generated – just the part creating test records – for a project that the actual implementation took about an hour of work :
I know it’s all super tiny but you get the idea, right ?
Alright, let’s go through it.
- You need to install Code-Robot in the production org you wish to “capture” data from. Head over to www.code-robot.com, and click on “Get in the AppExchange.” Install it like you would any other package from the AppExchange.Again: you must install it to the org you wish the create data from, not necessarily where you’ll be developing your test class.
- In Classic, select “Code Robot” from the AppMenu on the top right.
In Lightning, select “Code Robot Project” from the App Launcher on the top left.
- You’ll be greeted by the main Code-Robot window which looks like this:The only button we’re interested in right now is the “New Project” on the top right. You’ll then be asked to name your project and add a description.Of course, Projects allow to generate up-to-date data at will for any test class you previously generated code for.
- This is where the magic happens: From here, we are now able to select the actual records we want to replicate in our test code. Personally, I like to use the “Add Single Record” button, that allows you to just paste a record ID, and Code Robot then takes it from there.For this example, let’s go add an account. So we just click on “Accounts” and select the account we wish to use: Then we click on “Add Records” at the bottom.
- You can now see on the left that the record is added to the project:
If you click on the Account here, the view on the right will update to show the Lookups and Relationships available for this Record, and you can choose what needs to be added to the Test Class:
- All that’s left to do is to scroll down and click on “Generate Code”, to get your prize ! And by prize I mean test code:
- The generated code clearly shows where to insert your tests (That “todo” comment at line 25). You can see the variables created on top that contain the records – in this case, there’s only one for our Account. That’s all you need to get started building a test class using real, production org data.
Is that all you have to do?
Pretty much! In the project I mentioned at the beginning of this article, I had to comment out a line or two of data that would prevent the test from working because the sandbox I was deploying the test class to had some differences from the production environment where Code-Robot was used; some picklist values were different and validations rules were being changed.
So nothing that isn’t “normal” I would have had to take that into consideration if I had built the test class from scratch anyway, so yes, this is all you have to do.
Can I try it out?
That’s the beauty of it : As you saw from the last screenshot up there, our example here was built using the trial version of Code-Robot. The trial version offers full functionality up to a certain number of “code generation.” So it’s more than enough to get some good tests in place and really understand what it can do for your projects.
Anything else I should know?
There are some interesting ways to use Code-Robot that go beyond the obvious functions that I might get into in a future article, but here’s the short version:
Let’s say you have a record causing you problems with production, but you can’t replicate the issue in sandbox to do some test and debugging. You could use Code-Robot to bring that record to sandbox and create a test class that replicates the issue, at the same time, creating a very good – real-life tested – test scenario that all future code needs to be tested against.
Code-Robot is a very new tool. We had a chance to talk with the developers during TrailheaDX 2017, which happened to be their official launch and they’re very receptive to feedback. So if you test it out and find some issues with it, let them know; everyone will benefit from the improvements they can make to it.
That’s it! Now go make better unit tests!