I need help to write a test class for metadata api. Basically I'm using metadata api class to update the picklist values with list of user names. so whenever a user object is inserted the trigger will call the api class using future annotation. Now to deploy the code I need to write a test class for api.

Any inputs on how to develop the test class code featuring the use trigger calling the metadata api?

1 Answer 1


Your test should perform the DML that matches your trigger e.g. insert a User object.

Use Test.startTest before the insert and Test.stopTest after the insert to ensure that the future call is completed:

All asynchronous calls made after the startTest method are collected by the system.

Also write a WebServiceMock and use Test.setMock to wire that in to your test methods.

Below is an example of a WebServiceMock that handles a few MetadataService requests; the MetadataService.updateMetadata_element one might be all you need if you just want to test the happy path. But ideally you should assert the data passed into the mock and also return failures to check your error handling.

Note that this example assumes you are using this Apex Wrapper Salesforce Metadata API code; if you are not your code may need to be somewhat different.

public class MetadataServiceMock implements WebServiceMock {
    public void doInvoke(
            Object stub,
            Object request,
            Map<String, Object> response,
            String endpoint,
            String soapAction,
            String requestName,
            String responseNS,
            String responseName,
            String responseType
            ) {

        if (request instanceof MetadataService.readMetadata_element) {

            MetadataService.PicklistValue picklistValue = new MetadataService.PicklistValue();
            picklistValue.fullName = 'Pending';

            MetadataService.RecordTypePicklistValue recordTypePicklistValue = new MetadataService.RecordTypePicklistValue();
            recordTypePicklistValue.picklist = 'cve__Status__c';
            recordTypePicklistValue.values = new MetadataService.PicklistValue[] {picklistValue};

            MetadataService.RecordType recordType = new MetadataService.RecordType();
            recordType.fullName = 'cve__BenefitClaimed__c.cve__ShortTermDisability';
            recordType.picklistValues = new MetadataService.RecordTypePicklistValue[] {recordTypePicklistValue};

            MetadataService.ReadRecordTypeResult result = new MetadataService.ReadRecordTypeResult();
            result.records = new MetadataService.RecordType[] {recordType};

            MetadataService.readRecordTypeResponse_element responseElement = new MetadataService.readRecordTypeResponse_element();
            responseElement.result = result;

            response.put('response_x', responseElement);

        } else if (request instanceof MetadataService.updateMetadata_element) {

            MetadataService.SaveResult result = new MetadataService.SaveResult();
            result.success = true;

            MetadataService.updateMetadataResponse_element responseElement = new MetadataService.updateMetadataResponse_element();
            responseElement.result = new MetadataService.SaveResult[] {result};

            response.put('response_x', responseElement);

        } else if (request instanceof MetadataService.upsertMetadata_element) {

            MetadataService.UpsertResult result = new MetadataService.UpsertResult();
            result.success = true;

            MetadataService.upsertMetadataResponse_element responseElement = new MetadataService.upsertMetadataResponse_element();
            responseElement.result = new MetadataService.UpsertResult[] {result};

            response.put('response_x', responseElement);

        } else {
            System.assert(false, 'Unexpected request ' + request);
  • Keith -- I believe this answer presumes use of the Andrew Fawcett MetaData API Apex wrapper (?) From other posts by OP, it wasn't clear to me that he was using this wrapper.
    – cropredy
    Nov 23, 2015 at 19:00
  • @crop1645 OK, I've added that qualification to the answer. I wonder how different that code is these days from the default you would get generating your own Apex code.
    – Keith C
    Nov 23, 2015 at 19:10
  • good point -- the Fawcett wrapper gitHub included plenty of examples of testmethods, a resource that I used extensively to use as a model for my own testmethods
    – cropredy
    Nov 23, 2015 at 19:18
  • Thkx Keith, I generated the apex class form the API WSDL file and with the help of andrew's blog I had to fix the issues with the wsdl types that was not supported during the conversion. I will give a try and keep posted on it Nov 25, 2015 at 11:00

Not the answer you're looking for? Browse other questions tagged or ask your own question.