---
schema_version: "1"
id: bb17ce44abfadc79e7958bdd9dbdd10ce10d6a53c56fcbc3fa132b64875c7c8a
title: Testing Metadata Deployments
collection: legacydeveloper
version: current
locale: en-us
portal_url: https://docs.salesforce.com/legacydeveloper/current/en-us/apexcode/apex_metadata_testing.md
canonical_url: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_metadata_testing.htm
source_site: developer.salesforce.com
content_type: documentation
content_hash: 4f56346d1a4791391f1d2b2e5f1563276f95191f481eca0b13d8d45767399a17
description: Apex code that accesses metadata must be properly tested.
---
# Testing Metadata Deployments

Apex code that accesses metadata must be properly tested.

To provide Apex test coverage for metadata deployments, write tests that verify both the set up of the deployment request and handling of the deployment results.

Tests for deployment request code verify the metadata components and component values that get created and assert that the DeployContainer contains exactly what needs to be deployed.

Tests for deployment result code verify that your DeployCallback handles expected and unexpected results. Your DeployCallback is normally called by Salesforce as part of the asynchronous deployment process. Therefore, to test your callback outside of the deployment process, create tests that use your callback class directly. You also must create test DeployResults and DeployCallbackContext instances to test your DeployCallback.handleResults() method.

When creating a test instance of DeployCallbackContext, subclass DeployCallbackContext and provide your own implementation of getCallbackJobId().

// DeployCallbackContext subclass for testing that returns myJobId
public with sharing class TestingDeployCallbackContext extends Metadata.DeployCallbackContext {
  private Id myJobId = '000000000000000000'; // replace value with a job ID that you can use for testing
  public override Id getCallbackJobId() {
    return myJobId;
  }
}