Posts

Showing posts with the label unit testing

A Pattern for Apex Test Code So You Can Forget About Required Fields

Yeah, yeah, yeah, just take me to the code . I recently had the opportunity to start writing some code in a brand new org, which got me thinking about the best way to do test object creation. You know, that annoying problem where you need to generate an Account in a test class about 77 times. Here’s a pattern I came up with. This method involves some overhead but it has the advantages of (1) allowing you to set defaults for certain objects so you don’t have to worry about them – for some objects create them with all defaults with just one line, and (2) a generic, repeatable pattern. It’s based on an abstract class that implements the base operations using some generic sObject magic outlined here . A lot of code in this post, bear with me. (That's a basic version, for something more robust, look here .) For each object you will use, you need to create a class that implements the abstract factory class. Then, in your test code you would need to inst...