Skip to main content
deleted 108 characters in body
Source Link
Kris Goncalves
  • 22.8k
  • 8
  • 35
  • 72

You can set both types of mocks, which is the purpose of the first parameter.

@isTest static void testPageAction() {
  Test.setMock(WebServiceMock.class, new WSServicesMock());
  Test.setMock(HttpCalloutMock.class, new RestServicesMock());
  // rest of test

Please note that each mock may need to read the input parameters to determine the appropriate output, as you can only set one mock of each type. However, you can definitely respond to both types of callouts that may be in a single function call.

public class WSServicesMock 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) {
            // corrected syntax for switch expression, using 'switch on (requestName)' instead of 'switch (requestName)'
            switch on (requestName) {
                when 'abc' {
                    // handle first type
                }
                when 'xyz' {
                    // handle second type
                }
                when else {
                    // return an error?
                }
            }
        }
}

You can set both types of mocks, which is the purpose of the first parameter.

@isTest static void testPageAction() {
  Test.setMock(WebServiceMock.class, new WSServicesMock());
  Test.setMock(HttpCalloutMock.class, new RestServicesMock());
  // rest of test

Please note that each mock may need to read the input parameters to determine the appropriate output, as you can only set one mock of each type. However, you can definitely respond to both types of callouts that may be in a single function call.

public class WSServicesMock 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) {
            // corrected syntax for switch expression, using 'switch on (requestName)' instead of 'switch (requestName)'
            switch on (requestName) {
                when 'abc' {
                    // handle first type
                }
                when 'xyz' {
                    // handle second type
                }
                when else {
                    // return an error?
                }
            }
        }
}

You can set both types of mocks, which is the purpose of the first parameter.

@isTest static void testPageAction() {
  Test.setMock(WebServiceMock.class, new WSServicesMock());
  Test.setMock(HttpCalloutMock.class, new RestServicesMock());
  // rest of test

Please note that each mock may need to read the input parameters to determine the appropriate output, as you can only set one mock of each type. However, you can definitely respond to both types of callouts that may be in a single function call.

public class WSServicesMock 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) {
            
            switch on (requestName) {
                when 'abc' {
                    // handle first type
                }
                when 'xyz' {
                    // handle second type
                }
                when else {
                    // return an error?
                }
            }
        }
}
corrected syntax for switch expression, using 'switch on (requestName)' instead of 'switch (requestName)'
Source Link

You can set both types of mocks, which is the purpose of the first parameter.

@isTest static void testPageAction() {
  Test.setMock(WebServiceMock.class, new WSServicesMock());
  Test.setMock(HttpCalloutMock.class, new RestServicesMock());
  // rest of test

Please note that each mock may need to read the input parameters to determine the appropriate output, as you can only set one mock of each type. However, you can definitely respond to both types of callouts that may be in a single function call.

public class WSServicesMock 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) {
            // corrected syntax for switch expression, using 'switch on (requestName)' instead of 'switch (requestName)'
            switch on (requestName) {
                when 'abc' {
                    // handle first type
                }
                when 'xyz' {
                    // handle second type
                }
                when else {
                    // return an error?
                }
            }
        }
}

You can set both types of mocks, which is the purpose of the first parameter.

@isTest static void testPageAction() {
  Test.setMock(WebServiceMock.class, new WSServicesMock());
  Test.setMock(HttpCalloutMock.class, new RestServicesMock());
  // rest of test

Please note that each mock may need to read the input parameters to determine the appropriate output, as you can only set one mock of each type. However, you can definitely respond to both types of callouts that may be in a single function call.

public class WSServicesMock 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) {
            switch(requestName) {
                when 'abc' {
                    // handle first type
                }
                when 'xyz' {
                    // handle second type
                }
                when else {
                    // return an error?
                }
            }
        }
}

You can set both types of mocks, which is the purpose of the first parameter.

@isTest static void testPageAction() {
  Test.setMock(WebServiceMock.class, new WSServicesMock());
  Test.setMock(HttpCalloutMock.class, new RestServicesMock());
  // rest of test

Please note that each mock may need to read the input parameters to determine the appropriate output, as you can only set one mock of each type. However, you can definitely respond to both types of callouts that may be in a single function call.

public class WSServicesMock 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) {
            // corrected syntax for switch expression, using 'switch on (requestName)' instead of 'switch (requestName)'
            switch on (requestName) {
                when 'abc' {
                    // handle first type
                }
                when 'xyz' {
                    // handle second type
                }
                when else {
                    // return an error?
                }
            }
        }
}
Source Link
sfdcfox
  • 501.6k
  • 21
  • 473
  • 828

You can set both types of mocks, which is the purpose of the first parameter.

@isTest static void testPageAction() {
  Test.setMock(WebServiceMock.class, new WSServicesMock());
  Test.setMock(HttpCalloutMock.class, new RestServicesMock());
  // rest of test

Please note that each mock may need to read the input parameters to determine the appropriate output, as you can only set one mock of each type. However, you can definitely respond to both types of callouts that may be in a single function call.

public class WSServicesMock 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) {
            switch(requestName) {
                when 'abc' {
                    // handle first type
                }
                when 'xyz' {
                    // handle second type
                }
                when else {
                    // return an error?
                }
            }
        }
}