Salesforce HttpCalloutMock Interface implementation
The code snippet below sets two mocks responses that are returned in order.
-
Everything following a call to
next()
will be associated with the next response. -
A status code of 200 is set by default but may be overridden.
-
body()
can take a String or a Map which is then serialized. -
emptyBuilder()
will not create a starter response by default. You will need to add one vianext()
ormockCalloutException(String exceptionMessage)
. -
mockCalloutException(String exceptionMessage)
will create a mock that throws a Callout Exception.
Test.setMock(HttpCalloutMock.class, MockHttpResponse.builder()
.contentType('application/json')
.body('{ "status": "OK" }')
.next()
.statusCode(201)
.status('bibbity-bobbity')
.body('some text')
);
Test.setMock(HttpCalloutMock.class, MockHttpResponse.emptyBuilder()
.mockCalloutException('Oh noes!')
);
Test.setMock(HttpCalloutMock.class, MockHttpResponse.builder()
.contentType('application/json')
.body('{ "status": "OK" }')
.mockCalloutException('Oh noes!')
);