Can anyone help me understand how to write a test method/or generate mock response when using MetadataService.cls

Here is the line which I am trying to cover

MetadataService.ApprovalProcess approvalProcess
        = (MetadataService.ApprovalProcess) service.readMetadata(Constants.METADATA_API_TYPE_APPROVAL_PROCESS,
                                                                    new String[] { approvalProcessName }).getRecords()[0];

I am trying to generate a mock response for this callout. I tried generating a very basic response using

Test.setMock(HttpCalloutMock.class, new MockHttpResponseGenerator());

My MockHttpResponseGenerator looks like below

public class MockHttpResponseGenerator implements HttpCalloutMock {
    public HTTPResponse respond(HTTPRequest req) {
        HttpResponse res = new HttpResponse();
        res.setHeader('Content-Type', 'application/json');
        return res;

I am getting "Methods defined as TestMethod do not support Web service callouts" – I thought there would be some kind of global MockResponse implementation within MetadataService.cls which could be leveraged to generate mock response.

| improve this question | | | | |
  • What have you tried so far? Where are you stuck? You cannot test private methods directly, so you either need to manipulate other methods which in turn call this method, or it is not being used and you should remove it from your class. – Adrian Larson Jul 28 '17 at 0:34
  • Hey Adrian, this is snippet where I get exception during testing but you are right; I am calling this method from another apex class which is a public method. I am basically trying to have a mock response generated for this callout so I can cover this line MetadataService.ApprovalProcess approvalProcess = (MetadataService.ApprovalProcess) service.readMetadata(Constants.METADATA_API_TYPE_APPROVAL_PROCESS, new String[] { approvalProcessName }).getRecords()[0]; – Vinar Jul 28 '17 at 0:41
  • Please edit your post with any clarifications, especially what you have tried so far. – Adrian Larson Jul 28 '17 at 0:42
  • What does your test class look like? You need to call the method that eventually makes the fallout between test start and stop – Eric Jul 28 '17 at 3:07
  • Thanks guys, I was dumb to use HTTPMock instead of WebServiceMock. I was able to use the snippet posted below; modify it to my response type make it work. – Vinar Jul 31 '17 at 23:21

For MetadataService.cls you need to implement WebServiceMock instead of HttpCalloutMock.To create webservice mock follow the Link

Ex Webservicemock for connected app.

global with sharing class CallOutMock implements WebServiceMock{
global void doInvoke(
    Object stub,
    Object request,
    Map<String, Object> response,
    String endpoint,
    String soapAction,
    String requestName,
    String responseNS,
    String responseName,
    String responseType) {
        MetadataService.createMetadataResponse_element metadataResponse = new MetadataService.createMetadataResponse_element();
        MetadataService.SaveResult saveresult = new MetadataService.SaveResult();
        saveresult.errors = new List<MetadataService.Error>();
        saveresult.fullName = 'TestApp';
        saveresult.success = true;
        metadataResponse.result = new List<MetadataService.SaveResult>();
        response.put('response_x', metadataResponse); 
| improve this answer | | | | |
  • Thank you very much @ajay-prakash-dubey. Thats all the direction I needed. I was successfully able to implement WebServiceMock and everything flowed smoothly. – Vinar Jul 31 '17 at 23:17

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.