27 votes

How to create mock class for multiple Callouts in single class

This need is covered in Testing Apex Callouts using HttpCalloutMock. Specifically, see the Testing multiple HTTP callouts section. Here's the example they use: public class MultiRequestMock ...
user avatar
  • 144k
25 votes
Accepted

How to create mock class for multiple Callouts in single class

Here is perhaps the simplest form of a mock that deals with multiple requests. It is declared as an inner class of the test class so the idea is that it handles all the cases that the outer test ...
user avatar
  • 130k
17 votes
Accepted

How do I unit test code that includes a callout?

As part of the isolation of the test context, Salesforce does not allow your code to make REST or SOAP callouts during test execution. This includes all code that's executed in test context, even if ...
13 votes
Accepted

Salesforce 2020 release has blocked all Apex tests that use http mocking

This should now be fixed, Summer '20 patch 7 is live on almost all Summer 20 sandbox pods as of this edit This was caused by an attempt to fix a different callout testing related bug in Summer '20 (...
user avatar
  • 22.2k
12 votes
Accepted

Can we Have two Http Mock Callouts in a single test class method

Yes, you can, using either of two techniques. Your HttpCalloutMock's respond() method can inspect its inbound HttpRequest, and construct a different response based on which endpoint is being called, ...
user avatar
  • 87.3k
11 votes

HttpCallout error System.TypeException: Supplied type is not an interface

When you pass a Type to the Test.setMock method, you need to pass the interface being implemented, not the class that implements it. Joy Test.setMock(HttpCalloutMock.class, new ...
user avatar
  • 144k
9 votes
Accepted

Mock Testing with Named Credentials

Ahh, new Salesforce features. That said, if you're mocking, you're not really testing any of that functionality, just how the response is handled. You're also not calling a real server, so the ...
user avatar
7 votes
Accepted

HttpCallout error System.TypeException: Supplied type is not an interface

See Testing HTTP Callouts by Implementing the HttpCalloutMock Interface (emphasis added): For the first argument, pass HttpCalloutMock.class, and for the second argument, pass a new instance of your ...
user avatar
  • 17.8k
6 votes

how to test a future method which makes callouts?

Here is where Test.startTest() and Test.stopTest() come in to play. Structure your test to do all the data setup, then call startTest(). Execute your tested method and then call StopTest(). Among ...
user avatar
  • 6,998
6 votes
Accepted

Http callout test class "uncommitted work pending" error with no clear cause

Since you said in your comment that disabling workflows allows it to pass I am going to assume that some of those workflows are email alerts. This is a know issue which I first reported back in April ...
user avatar
  • 53.2k
6 votes
Accepted

Is it mandatory to write HttpCalloutMock?

The entire point of unit tests has nothing to do with coverage, but everything to do with avoiding regression bugs. By simply skipping testing, you're actually putting yourself at risk that something ...
user avatar
  • 438k
5 votes
Accepted

Issue with Email Alerts and Test.setMock - Uncommitted Work Pending

This may be a known issue -- recently reported and contemporaneous with your OP
user avatar
  • 63k
5 votes
Accepted

What is the difference in HTTP callout and SOAP & REST api?

An HTTP callout is code that you write within Salesforce to (generally) call out to an external API over HTTP. REST and SOAP are types of APIs. So, you could make a callout to an external SOAP API ...
user avatar
  • 7,537
5 votes
Accepted

test class with @auraenabled methods in apex class

The same way you'd test any other code, by executing the method under test. For example: @AuraEnabled public static String TestController(String username, String password) { try { String ...
user avatar
  • 3,811
5 votes

Mock Service .setBody()

Test.setMock(HttpCalloutMock.class, fakeResponse); Test.setMock(HttpCalloutMock.class, fakeReturn500); You can only set one mock per interface at a time. Your mock class has to be able to send ...
user avatar
  • 438k
5 votes
Accepted

How to set an HttpCalloutMock to reply differently on subsequent requests to the same endpoint?

This test works just fine for me: @IsTest class Demo { class Mock implements HttpCalloutMock { Boolean hasResponded = false; public HttpResponse respond(HttpRequest request) ...
user avatar
  • 144k
5 votes
Accepted

How can I write test classes for HTTP callouts which has checks for multiple status codes?

Write test-method for each possible scenario. For example, you have the following conditions: if(A) { if(subA) { do A and subA; } else { do A and not SubA; } } else { ...
user avatar
5 votes

Is it mandatory to write HttpCalloutMock?

Yes, implementing a mock should be considered necessary. If you do not care about the response, it is quite simple: @IsTest public class MyMock implements HttpMock { public HttpResponse respond(...
user avatar
  • 144k
5 votes
Accepted

The Test.isRunningTest() function is not working

Your code in test execution is failed at line response = ht_tp.send(request);. Because you have try-catch block, which catches exceptions, you were not able to observe this exception during test ...
user avatar
4 votes

Write Test class for custom controller having wrapper class that makes a HTTP callout

You need to Mock the HTTP response. The official Apex documentation has plenty of examples doing that. A good starting point is Testing HTTP Callouts .
user avatar
  • 560
4 votes
Accepted

Mock Unit Test for a REST API callout

Below is the sort of test class I would write for this situation. I assume the aim is to test your own code not the 3rd party code so this aims to do the minimum to get the sequence of requests to ...
user avatar
  • 130k
4 votes

Method defined as testMethod do not support web service callouts, test skipped

Methods defined as TestMethod do not support Web service callouts Stack Trace: null This error occurs because web service callouts are not allowed in Test class. To bypass callouts add ...
user avatar
  • 141
4 votes

Test Class Error for web service callouts "System.CalloutException: You have uncommitted work pending. Please commit or rollback before calling out"

Looks like you have the right components in the test but in the wrong order? You're using the Test.setMock() after the Insert DN; (and that insert has the side-effect of making the callout). Read ...
user avatar
  • 23.6k
4 votes

Unit Testing Callout with Custom Setting

As an alternative to Daniel's great answer, one thing that we do at my org is created a Settings class to handle the fetching of settings. Methods within this class always return an instance of the ...
user avatar
  • 14.5k
4 votes

"Callout loop not allowed" in unit test for Queueable that implements HttpCalloutMock

I believe you're looking at a Platform bug - as far as I can tell, setMock is not working for callouts in Queueable Apex classes at this time. I've filed a case and am waiting to hear back. Update 8/...
user avatar
  • 3,384
4 votes

Why Batch can't work in unit-test?

This is a known bug in the mock implementation and a search would have return several results. This one being the most relevant: Getting CalloutException when testing Batchable class with Callouts ...
user avatar
  • 53.2k
4 votes
Accepted

Queueable class throws Uncommitted Work Pending error in one test, but not others

Found a possible explaination here: Uncommitted work pending in unit test with trigger and queueable callout Looks like queueing the job counts as a DML
user avatar
4 votes
Accepted

How can I test my Web Token and posted record from Post Callout?

Your problem is that you're not returning an object with an access_token value. What you really need is two separate responses. In other words, your mock class has to determine which response to send ...
user avatar
  • 438k
4 votes
Accepted

Methods defined as TestMethod do not support Web service callouts - Unit Test

You can disable Process Builders in apex by adding a switch. For this you have to create a custom setting (protected) which have a boolean field (with default value true). Then update your Process ...
user avatar
  • 1,540
4 votes
Accepted

System.XmlException: start tag unexpected character =

There are few things wrong with the xml. 1) In Apex, the string starts and end with single quotes ' , thus you don't need to escape " 2) You cannot use & in XML, it makes XML invalid, you have ...
user avatar

Only top scored, non community-wiki answers of a minimum length are eligible