0

I tried creating a mock class for a class that uses MetadataService but after too many attempts, I stll can't get it. Can anyone please give me a sample mock class for these lines?

MetadataService.MetadataPort service = MetadataServiceExamples.createService();
(MetadataService.CustomField) service.readMetadata('CustomField', new String[] { fieldName }).getRecords()[0];

for MetadataServiceExamples.createService():

public static MetadataService.MetadataPort createService()
{
    MetadataService.MetadataPort service = new MetadataService.MetadataPort();
    service.SessionHeader = new MetadataService.SessionHeader_element();
    service.SessionHeader.sessionId = UserInfo.getSessionId();
    return service;
}

I just dont know what to do anymore. I hope anuone can give me a sample to get the idea.

  • 2
    How are we supposed to help you if we don't know what the problem is? It's great that you shared your code, but without a specific error, it may be hard to diagnose without an error. – sfdcfox Jul 6 at 3:39
1

Here is a sample MetadataService mock class:

/**
 * Basic mock for the operations presently used.
 */
public class MetadataServiceMock implements WebServiceMock {

    public static final String[] RECORD_TYPE_FULL_NAMES = new String[] {
        'cve__BenefitClaimed__c.cve__ShortTermDisability'
    };
    public static final String[] CUSTOM_FIELD_FULL_NAMES = new String[] {
        'Contact.Email',
        'cve__BenefitClaimed__c.cve__DateOfDiagnosis__c'
    };

    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) {
            response.put('response_x', readResponse(request));
        } else if (request instanceof MetadataService.updateMetadata_element) {
            response.put('response_x', updateResponse(request));
        } else if (request instanceof MetadataService.upsertMetadata_element) {
            response.put('response_x', upsertResponse(request));
        } else {
            System.assert(false, 'Unexpected request ' + request);
        }
    }

    // Implements couple of requests
    private MetadataService.IReadResponseElement readResponse(Object request) {

        MetadataService.readMetadata_element r = (MetadataService.readMetadata_element) request;

        if (r.type_x == 'RecordType') {

            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 = RECORD_TYPE_FULL_NAMES[0];
            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;

            return responseElement;

        } else if (r.type_x == 'CustomField') {

            MetadataService.ReadCustomFieldResult result = new MetadataService.ReadCustomFieldResult();
            result.records = new MetadataService.CustomField[] {
                createContactEmailField(),
                createBenefitClaimedDateOfDiagnosisField()
            };

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

            return responseElement;

        } else {

            System.assert(false, 'Unexpected read type ' + r.type_x);
            return null;
        }
    }

    public static MetadataService.CustomField createContactEmailField() {

        MetadataService.CustomField f = new MetadataService.CustomField();
        f.type_x = String.valueOf(DisplayType.Email);
        f.fullName = CUSTOM_FIELD_FULL_NAMES[0];
        f.label = 'Email';
        f.length = 80;
        f.encrypted = false;

        return f;
    }

    public static MetadataService.CustomField createBenefitClaimedDateOfDiagnosisField() {

        MetadataService.CustomField f = new MetadataService.CustomField();
        f.type_x = String.valueOf(DisplayType.Date);
        f.fullName = CUSTOM_FIELD_FULL_NAMES[1];
        f.label = 'Date';
        f.encrypted = false;

        return f;
    }

    // Does nothing except return success
    private MetadataService.updateMetadataResponse_element updateResponse(Object request) {

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

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

        return responseElement;
    }

    // Does nothing except return success
    private MetadataService.upsertMetadataResponse_element upsertResponse(Object request) {

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

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

        return responseElement;
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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