SUPPRESSWARNINGS
The MockDatabase class is used to simulate the Salesforce database in Apex unit tests
Since
3/23/2024
Group Mock Database
Author Zackary Frazier
public static dmlHandler
public static soqlHandler
Perform an aggregate query
public static List<Aggregate> aggregateQuery(String query)
Name | Type | Description |
---|---|---|
query | String | String |
List<Aggregate>
,[object Object]
Perform an aggregate query with access level
public static List<Aggregate> aggregateQuery(String query, System.AccessLevel accessLevel)
Name | Type | Description |
---|---|---|
query | String | String |
accessLevel | System.AccessLevel | System.AccessLevel |
List<Aggregate>
,[object Object]
Perform an aggregate query with binds
public static List<Aggregate> aggregateQueryWithBinds(String query, Map<String,Object> binds, System.AccessLevel accessLevel)
Name | Type | Description |
---|---|---|
query | String | String |
binds | Map<String,Object> | Map<String, Object> |
accessLevel | System.AccessLevel | System.AccessLevel |
List<Aggregate>
,[object Object]
Perform a count query
public static Integer countQuery(String query)
Name | Type | Description |
---|---|---|
query | String | String |
Integer
,[object Object]
Perform a count query with access level
public static Integer countQuery(String query, System.AccessLevel accessLevel)
Name | Type | Description |
---|---|---|
query | String | String |
accessLevel | System.AccessLevel | System.AccessLevel |
Integer
,[object Object]
Perform a count query with binds
public static Integer countQueryWithBinds(String query, Map<String,Object> binds, System.AccessLevel accessLevel)
Name | Type | Description |
---|---|---|
query | String | String |
binds | Map<String,Object> | Map<String, Object> |
accessLevel | System.AccessLevel | System.AccessLevel |
Integer
,[object Object]
Deletes a record from the mock database
public static Database.DeleteResult doDelete(SObject recordToDelete, Boolean allOrNone)
Name | Type | Description |
---|---|---|
recordToDelete | SObject | - The record to delete |
allOrNone | Boolean | - Whether to throw an exception if the record does not exist |
Database.DeleteResult
Database.DeleteResult - The result of the delete operation
doDelete is used to delete a list of records from the database
public static List<Database.DeleteResult> doDelete(List<SObject> recordsToDelete, Boolean allOrNone)
Name | Type | Description |
---|---|---|
recordsToDelete | List<SObject> | - The records to delete |
allOrNone | Boolean | - Whether to throw an exception if any of the records do not exist |
List<Database.DeleteResult>
List<Database.DeleteResult> - The results of the delete operations
doDelete is used to delete a record from the database
public static Database.DeleteResult doDelete(Id recordID, Boolean allOrNone)
Name | Type | Description |
---|---|---|
recordID | Id | - The Id of the record to delete |
allOrNone | Boolean | - Whether to throw an exception if the record does not exist |
Database.DeleteResult
Database.DeleteResult - The result of the delete operation
doDelete is used to delete a list of records from the database
public static List<Database.DeleteResult> doDelete(List<Id> recordIDs, Boolean allOrNone)
Name | Type | Description |
---|---|---|
recordIDs | List<Id> | - The Ids of the records to delete |
allOrNone | Boolean | - Whether to throw an exception if any of the records do not exist |
List<Database.DeleteResult>
List<Database.DeleteResult> - The results of the delete operations
doInsert is used to insert a record into the database
public static Database.SaveResult doInsert(SObject recordToInsert, Boolean allOrNone, System.AccessLevel accessLevel)
Name | Type | Description |
---|---|---|
recordToInsert | SObject | - The record to insert |
allOrNone | Boolean | - Whether to throw an exception if the record already exists |
accessLevel | System.AccessLevel | - The access level of the user performing the operation |
Database.SaveResult
Database.SaveResult - The result of the insert operation
doInsert is used to insert a list of records into the database
public static Database.SaveResult doInsert(SObject recordToInsert, Boolean allOrNone)
Name | Type | Description |
---|---|---|
recordToInsert | SObject | - The record to insert |
allOrNone | Boolean | - Whether to throw an exception if any of the records already exist |
Database.SaveResult
List<Database.SaveResult> - The results of the insert operations
doInsert is used to insert a list of records into the database
public static List<Database.SaveResult> doInsert(List<SObject> recordsToInsert, Boolean allOrNone)
Name | Type | Description |
---|---|---|
recordsToInsert | List<SObject> | - The records to insert |
allOrNone | Boolean | - Whether to throw an exception if any of the records already exist |
List<Database.SaveResult>
List<Database.SaveResult> - The results of the insert operations
doInsert is used to insert a list of records into the database
public static List<Database.SaveResult> doInsert(List<SObject> recordsToInsert, Boolean allOrNone, System.AccessLevel accessLevel)
Name | Type | Description |
---|---|---|
recordsToInsert | List<SObject> | - The records to insert |
allOrNone | Boolean | - Whether to throw an exception if any of the records already exist |
accessLevel | System.AccessLevel | - The access level of the user performing the operation |
List<Database.SaveResult>
List<Database.SaveResult> - The results of the insert operations
DML methods
public static void doMockInsert(SObject recordToInsert)
Name | Type | Description |
---|---|---|
recordToInsert | SObject |
void
doMockInsert is used to insert a list of records into the mock database without setting the wasCalled
flag
public static void doMockInsert(List<SObject> recordsToInsert)
Name | Type | Description |
---|---|---|
recordsToInsert | List<SObject> | - The records to insert |
void
SOQL methods
public static List<SObject> doSelectQueryInternal(Node head, Map<String,Object> params)
Name | Type | Description |
---|---|---|
head | Node | |
params | Map<String,Object> |
List<SObject>
doUndelete is used to undelete a record from the database
public static Database.UndeleteResult doUndelete(sObject recordToUndelete, Boolean allOrNone)
Name | Type | Description |
---|---|---|
recordToUndelete | sObject | - The record to undelete |
allOrNone | Boolean | - Whether to throw an exception if the record does not exist |
Database.UndeleteResult
Database.UndeleteResult - The result of the undelete operation
DmlException: - If the record does not exists and allOrNone is true
doUndelete is used to undelete a list of records from the database
public static List<Database.UndeleteResult> doUndelete(List<sObject> recordsToUndelete, Boolean allOrNone)
Name | Type | Description |
---|---|---|
recordsToUndelete | List<sObject> | - The records to undelete |
allOrNone | Boolean | - Whether to throw an exception if any of the records do not exist |
List<Database.UndeleteResult>
List<Database.UndeleteResult> - The results of the undelete operations
DmlException: - If any of the records do not exists and allOrNone is true
doUndelete is used to undelete a record from the database
public static Database.UndeleteResult doUndelete(Id recordID, Boolean allOrNone)
Name | Type | Description |
---|---|---|
recordID | Id | - The Id of the record to undelete |
allOrNone | Boolean | - Whether to throw an exception if the record does not exist |
Database.UndeleteResult
Database.UndeleteResult - The result of the undelete operation
doUndelete is used to undelete a list of records from the database
public static List<Database.UndeleteResult> doUndelete(List<Id> recordIDs, Boolean allOrNone)
Name | Type | Description |
---|---|---|
recordIDs | List<Id> | - The Ids of the records to undelete |
allOrNone | Boolean | - Whether to throw an exception if any of the records do not exist |
List<Database.UndeleteResult>
List<Database.UndeleteResult> - The results of the undelete operations
DmlException: - If any of the records do not exists and allOrNone is true
doUndelete is used to undelete a record from the database
public static Database.UndeleteResult doUndelete(SObject recordToUndelete, Boolean allOrNone, System.AccessLevel accessLevel)
Name | Type | Description |
---|---|---|
recordToUndelete | SObject | - The record to undelete |
allOrNone | Boolean | - Whether to throw an exception if the record does not exist |
accessLevel | System.AccessLevel | - The access level of the user performing the operation |
Database.UndeleteResult
Database.UndeleteResult - The result of the undelete operation
doUpdate is used to update a record in the database
public static Database.SaveResult doUpdate(SObject recordToUpdate, Boolean allOrNone)
Name | Type | Description |
---|---|---|
recordToUpdate | SObject | - The record to update |
allOrNone | Boolean | - Whether to throw an exception if the record does not exist |
Database.SaveResult
Database.SaveResult - The result of the update operation
DmlException: - If the record does not exists and allOrNone is true
doUpdate is used to update a list of records in the database
public static List<Database.SaveResult> doUpdate(List<SObject> recordsToUpdate, Boolean allOrNone)
Name | Type | Description |
---|---|---|
recordsToUpdate | List<SObject> | - The records to update |
allOrNone | Boolean | - Whether to throw an exception if any of the records do not exist |
List<Database.SaveResult>
,[object Object], - The results of the update operations
Updates a record in the mock database
public static Database.SaveResult doUpdate(SObject recordToUpdate, Boolean allOrNone, System.AccessLevel accessLevel)
Name | Type | Description |
---|---|---|
recordToUpdate | SObject | Record to update |
allOrNone | Boolean | Boolean to throw an exception if the record does not exist |
accessLevel | System.AccessLevel | AccessLevel of the user performing the operation |
Database.SaveResult
,[object Object]
Update a list of records in the mock database
public static List<Database.SaveResult> doUpdate(List<SObject> recordsToUpdate, Boolean allOrNone, System.AccessLevel accessLevel)
Name | Type | Description |
---|---|---|
recordsToUpdate | List<SObject> | Records to update |
allOrNone | Boolean | Boolean to throw an exception if the record does not exist |
accessLevel | System.AccessLevel | Access Level of the user performing the operation |
List<Database.SaveResult>
,[object Object]
Upserts a record into the mock database
public static Database.UpsertResult doUpsert(SObject recordToUpsert, SObjectField externalIdField, Boolean allOrNone)
Name | Type | Description |
---|---|---|
recordToUpsert | SObject | Records to upsert |
externalIdField | SObjectField | Optional field to use as the external ID |
allOrNone | Boolean | Boolean to throw an exception if the record does not exist |
Database.UpsertResult
,[object Object]
Upserts a record in the mock database
public static List<Database.UpsertResult> doUpsert(List<SObject> recordsToUpsert, SObjectField externalIdField, Boolean allOrNone)
Name | Type | Description |
---|---|---|
recordsToUpsert | List<SObject> | Records to upsert |
externalIdField | SObjectField | Optional field to use as the external ID |
allOrNone | Boolean | Boolean to throw an exception if the record does not exist |
List<Database.UpsertResult>
,[object Object]
SUPPRESSWARNINGS
Upserts a record in the mock database
public static Database.UpsertResult doUpsert(SObject recordToUpsert, SObjectField externalIdField, Boolean allOrNone, System.AccessLevel accessLevel)
Name | Type | Description |
---|---|---|
recordToUpsert | SObject | Record to upsert |
externalIdField | SObjectField | Optional external ID field |
allOrNone | Boolean | Boolean to throw an exception if the record does not exist |
accessLevel | System.AccessLevel | Access level of the user performing the operation |
Database.UpsertResult
,[object Object]
SUPPRESSWARNINGS
Upsert a list of records in the mock database
public static List<Database.UpsertResult> doUpsert(List<SObject> recordsToUpsert, SObjectField externalIdField, Boolean allOrNone, System.AccessLevel accessLevel)
Name | Type | Description |
---|---|---|
recordsToUpsert | List<SObject> | Records to upsert |
externalIdField | SObjectField | Optional external ID field |
allOrNone | Boolean | Boolean to throw an exception if the record does not exist |
accessLevel | System.AccessLevel | Access level of the user performing the operation |
List<Database.UpsertResult>
,[object Object]
This method is used to query the mock database by traversing the AST
public static List<SObject> query(String query)
Name | Type | Description |
---|---|---|
query | String | : the query string |
List<SObject>
: the query results
Query the database for records, with access level
public static List<SObject> query(String query, System.AccessLevel accessLevel)
Name | Type | Description |
---|---|---|
query | String | String |
accessLevel | System.AccessLevel | System.AccessLevel |
List<SObject>
,[object Object]
Query the database for records, with binds
public static List<SObject> queryWithBinds(String query, Map<String,Object> binds, System.AccessLevel accessLevel)
Name | Type | Description |
---|---|---|
query | String | String |
binds | Map<String,Object> | Map<String, Object> |
accessLevel | System.AccessLevel | System.AccessLevel |
List<SObject>
,[object Object]