Test Your Metadata Deployment

Learning Objectives

After completing this unit, you’ll be able to:
  • Describe what you can and can’t test for a metadata deployment.
  • Explain how to test a metadata container.
  • Explain how to test a metadata callback.

Test Your Metadata Deployment

No Apex code is complete without test coverage. And so, any code you write using Apex Metadata API must be properly tested. Let’s look at what you can test (and what you can’t).

The main focus of your testing for metadata deployments is the setup of the deployment request and the handling of the deployment results. To verify the request, you can test that the deployment container is set up properly. To test the deployment results, you can test that your deployment callback behaves as expected.

You can’t actually test how the org looks after the deployment. But testing the container and callback is a good way to ensure that everything’s working properly.

Test Your Deployment Container

To test your deployment request code, you can verify the metadata components and component values that get created. Use assert statements to ensure that the deployment container contains exactly what you expect.

The following code gives you an idea of what a test class could look like for the DeployMetadata class that we created in unit 2. The testDeployment method creates a deployment request and retrieves the layout metadata that we created. You can then navigate through the metadata to make sure it contains the custom field we created.

  1. Open the Developer Console by clicking the Setup gear, then Developer Console.
  2. In the Developer Console, click File | New | Apex Class.
  3. Name the class DeploymentTest and click OK.
  4. Get rid of any existing code, and copy and paste the following example code in its place.
public class DeploymentTest {
    static void testDeployment() {
        DeployMetadata deployMd = new DeployMetadata();
        Metadata.DeployContainer container = deployMd.constructDeploymentRequest();
        List<Metadata.Metadata> contents = container.getMetadata();
        System.assertEquals(1, contents.size());
        Metadata.Layout md = (Metadata.Layout) contents.get(0);
        // Perform various assertions the layout metadata.
        System.assertEquals('Account-Account Layout', md.fullName);

Test Your Deployment Callback

To test your deployment results, verify that your callback handles expected and unexpected results. Your callback is normally called by Salesforce as part of the asynchronous deployment process. So to test your callback outside of the deployment process, create tests that use your callback class directly.

Here’s what a test class can look like for the PostInstallCallback class that we created in unit 2. The code instantiates the callback, and creates test instances of DeployResults and DeployCallbackContext objects. It uses these test instances to verify the DeployCallback.handleResults method.

From the Developer Console, create an Apex class with the name MyDeploymentCallbackTest and the following code.

public class MyDeploymentCallbackTest {
    static void testMyCallback() {
        // Instantiate the callback.
        Metadata.DeployCallback callback = new PostInstallCallback();
        // Create test result and context objects.
        Metadata.DeployResult result = new Metadata.DeployResult();
        result.numberComponentErrors = 1;
        Metadata.DeployCallbackContext context = new Metadata.DeployCallbackContext();
        // Invoke the callback's handleResult method.
        callback.handleResult(result, context);

In this example, DeployCallbackContext returns a null job ID (through the getCallbackJobId method). If you need a non-null value, you can create a subclass of DeployCallbackContext and provide your own implementation of the getCallbackJobId method. For example:

// DeployCallbackContext subclass for testing that returns myJobId.
public class TestingDeployCallbackContext extends Metadata.DeployCallbackContext {
    private Id myJobId = null; // Set to a fixed ID you can use for testing.
    public override Id getCallbackJobId() {
        return myJobId;


Apex Metadata API wasn’t built for testing other code. It can be tempting to use the Apex Metadata API to toggle features on and off for unit testing. However, because Apex Metadata API relies on asynchronous deployment, it isn’t compatible with Apex’s synchronous tests.


Apex Developer Guide: Testing Metadata Deployments

Continue a aprender de graça!
Inscreva-se em uma conta para continuar.
O que você ganha com isso?
  • Receba recomendações personalizadas para suas metas de carreira
  • Pratique suas habilidades com desafios práticos e testes
  • Monitore e compartilhe seu progresso com os empregadores
  • Conecte-se a orientação e oportunidades de carreira