Skip to content

Test Data Builder Field Injection

John Storey edited this page May 16, 2020 · 4 revisions

Situation

A downstream package has added a field value requirement, as in the field is required or must have a certain value or range of values, etc. In this case, the Marketing implementation has declared the "Slogan" field on the Account is required. If DML-based tests are necessary, peer or upstream packages cannot be aware of this requirement, and downstream packages should not be troubled with irrelevant requirements.

Answer

Reading from a custom metadata type, a TestDataSupplementer, existing in the core implementation, can dynamically call implementation appropriate classes that refine test data in order to alleviate data-requirement rules.

Details

  • A unit test creates an instance of an Account and a passes it to the TestDataSupplementer.
  • During instantiation, the TestDataSupplementer reads the TestDataSupplementer custom metadata type for records related to the Account.
  • The supplementer then pass the test's data to the specified classes in the custom metadata.
  • The Slogan field is populated after the test class calls the TestDataSupplementer, although the test is unaware of any such requirement. Note: This project does not in fact define the Slogan field as required, but the test does not populate the field, although the assertion validates its existence.

References: