How to mock MetadataService.cls responses in testmethods – Collection store exception

I am using MetadataServiceAPI writing by Andrew Fawcett. Please see
https://github.com/financialforcedev/apex-mdapi/blob/master/apex-mdapi/src/classes/MetadataServiceExamples.cls

I wrote Unit Test case to mock up the webservice call for readCustomObject().

private class WebServiceMockImpl 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){
            System.debug('starting ...');
            MetadataService.readMetadataResponse_element responseElement = new MetadataService.readMetadataResponse_element();
            MetadataService.ReadResult readResult = new MetadataService.ReadResult();
            readResult.records = createCustomObjects(); 
            System.debug('records=' +readResult  );

            responseElement.result = readResult ;
            response.put('response_x',responseElement);
            System.debug('end ...');
            //Collection store exception putting MUSW.MetadataService.readMetadataResponse_element into MAP<String,MUSW.MetadataService.IReadResponseElement>
        }

        return;
    }
}

please also see the createCustomObjects method.

private static MetadataService.CustomObject[] createCustomObjects(){
    MetadataService.CustomObject[] objects = new MetadataService.CustomObject[]{};

    MetadataService.CustomObject obj = new MetadataService.CustomObject();
    obj.fullName = 'Contact';
    obj.listViews = createListViews();
    objects.add(obj);

    MetadataService.CustomObject obj2 = new MetadataService.CustomObject();
    obj2.fullName = 'Account';
    obj2.listViews = createListViews();
    objects.add(obj2);

    MetadataService.CustomObject obj3 = new MetadataService.CustomObject();
    obj3.fullName = 'MUSW__Inspection__c';
    obj3.listViews = createListViews();
    objects.add(obj3);

    return objects;

} 

After running the test class, it throws the following exception.

Collection store exception putting MUSW.MetadataService.readMetadataResponse_element into MAP.

The exception line is response.put(‘response_x’,responseElement)
Can anybody tell me how to fix it?

any wrong in the line readResult.records = createCustomObjects();

Thanks.

Answer

Having wrestled with this issue recently, I thought I would post the answer here as this has dropped off for a while. (There are a lot of types in MetadataService.cls and it is easy to get confused)

private class WebServiceMockImpl 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) {

    // test for the type of the response, OP was just doing readMetaData()
    if(request instanceof MetadataService.readMetadata_element){
      MetadataService.readMetadata_element requestReadMetadata_element  = 
            (MetadataService.readMetadata_element) request;
      // This allows you to generalize the mock response by type of metadata read
      if (requestReadMetadata_element.type_x == 'CustomObject') { 
         MetadataService.readCustomObjectResponse_element mockRes   = 
              new MetadataService.readCustomObjectResponse_element();
         mockRes.result = new MetaDataService.ReadCustomObjectResult();
         mockRes.result.records = createCustomObjects();
         response.put('response_x', mockRes);
      }
      else if (requestReadMetadata_element.type_x == 'CustomField') { 
        // mock your customField results}
      }
   }
   else if (request instanceof MetadataService.updateMetadata_element) {
    // mock the updateMetadata() response 
   }
   // else if (...) any other response you have to mock
   return;
 }
}

Attribution
Source : Link , Question Author : user8671 , Answer Author : cropredy

Leave a Comment