Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

DML mocking, CRUD mocking, dependency injection framework for Salesforce.com (SFDC) using Apex

License

NotificationsYou must be signed in to change notification settings

jamessimone/apex-dml-mocking

Repository files navigation

Welcome to the SFDX project home for blazing fast Apex unit tests! For your consideration, this is anexample of how to implement the full CRUD (Create Read Update Delete) mocking implementation within your own Salesforce orgs. You can find out more information by perusing:

Writing tests that scale as the size of your organization grows is an increasingly challenging problem in the Salesforce world. It's not uncommon in large companies for deploys to last several hours; the vast majority of that time is spent running tests to verify that your code coverage is good enough for the deploy to succeed. Tests don'tneed to take that long.

This repo shows you how you can mock your SOQL queries and DML statements in Apex by using lightweight wrappers that are dependency injected into your business logic objects. This allows you to replace expensive test setup and test teardown with a fake database. I've used this method to cut testing time down by 90%+ -- in a small org, with only a few hundred tests, running tests and deploying can be done in under five minutes (easily). In large orgs, with many hundreds or thousands of tests, overall testing time tends to scale more linearly with organizational complexity; there are additional optimizations that can be done in these orgs to keep deploys in the 10 minute(s) range.

Access Level & DML Option Setting

BothIDML andIRepository instances returned by the framework support the methodIDML setOptions(Database.DMLOptions options, System.AccessLevel accessLevel);. Note that if DML options are not set by default, this framework uses true for theallOrNone value when performing DML, as that is consistent with the standard for calling DML operations without specifying that property. Please also note that DML options are not "expired" after having been set -- if you are using an instance ofIRepository orIDML and are performing multiple DML operations using that same instance, the DML options that have been set will continue to apply to subsequent operations untilsetOptions is re-called, or a new instance is initialized. DML options arenot shared between instances; they are not statically set. Passingnull for the DML options value will only update the access level.

By default, all operations are run usingSystem.AccessMode.SYSTEM_MODE. You can either override this (forIDML andIRepository instances) by callingsetOptions, as shown above, or by callingIRepository setAccessLevel(System.AccessLevel accessLevel); onIRepository instances. Like DML options, the access level that is set for an instance is then the one used for subsequent operations involving that repository instance.

DML Mocking Basics

Try checking out the source code for the DML wrapping classes:

SOQL Mocking Basics

Take a look at the following classes to understand how you can replace raw SOQL in your code with testable (and extendable) strongly typed queries:

Then, move on to the more complicated examples:

While opinionated in implementation, these classes are also just scratching the surface of what's possible when taking advantage of the Factory pattern in combination with the Repository pattern, including full support for:

  • strongly typed subqueries (queries returning children records)
  • strongly typed parent-level fields
  • the ability to easily extend classes likeRepository to include things like limits, order bys, etc ...

Dependency Injection Basics

The "Factory" pattern is of particular importance for DML mocking, because it allows you to have onlyone stub in your code for deciding whether or not to use mocks when running tests; crucially, the stub is only available when tests are being run: you cannot mock things in production-grade code.

You can have as many Factories as you'd like. I like to break my Factories out by responsibility:

  • A factory for Trigger handlers
  • Afactory for basic classes
  • TheRepoFactory for CRUD related objects

It's a pretty standard approach. You might choose to break things down by (business) domain. There's no right way.

Package-Based Development

These repository (as of 18 May 2023) has been slightly reworked to provide better support for package-based development. The updates are primarily to show how theexample-app folder can be in a completely separate package while still allowing for strongly-typed references (and package-specific factories and repo factories) to be referenced properly. For a concrete example, check out:


More Information

For more information on these patterns and how to use them, consider the free resources I've published underThe Joys Of Apex. Thanks for your time!

About

DML mocking, CRUD mocking, dependency injection framework for Salesforce.com (SFDC) using Apex

Topics

Resources

License

Code of conduct

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors7


[8]ページ先頭

©2009-2025 Movatter.jp