5

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.

  • Why does it report that the response Map is of type MAP<String,MUSW.MetadataService.IReadResponseElement>? It should be Map<String, Object> as the parameters for doInvoke define. – Daniel Ballinger Jun 10 '14 at 0:05
  • yes, you are right. I missed copying the complete error message. MAP<String,MUSW.MetadataService.IReadResponseElement>. The error happens in the line response.put('response_x',responseElement); Any solutions? any wrong in the line readResult.records = createCustomObjects(); – user8671 Jun 10 '14 at 0:27
  • The error suggests that the doInvoke parameter response isn't of the type shown in your example code. Does the code response.put('response__x', 'foobar'); give the same error? If so, check the code for anything that code be changing the definition of the response map. – Daniel Ballinger Jun 10 '14 at 0:35
  • Yes, give the same error after use 'foobar' in response.put map. The purpose here is I want to response a custom object. I used createCustomObject() to mockup an object. Any wrong in the lines: MetadataService.readMetadataResponse_element responseElement = new MetadataService.readMetadataResponse_element(); MetadataService.ReadResult readResult = new MetadataService.ReadResult(); readResult.records = createCustomObjects(); responseElement.result = readResult ; – user8671 Jun 10 '14 at 0:54
  • If you are certain that your response is a Map<String, Object>, try adding: System.assert(responseElement instanceof MUSW.MetadataService.IReadResponseElement); before putting it in the map. If the assertion fails, look at your definition of MetadataService.readMetadataResponse_element and see where that interface is defined. – Daniel Ballinger Jun 10 '14 at 1:26
2

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;
 }
}
| improve this answer | |

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.