Questions tagged [httpcalloutmock]

The HttpCalloutMock interface enables sending fake responses when testing HTTP callouts in Salesforce.

Filter by
Sorted by
Tagged with
24
votes
4answers
6k views

Unit tests that need more than one WebServiceMock

I'm dealing with an odd situation where I need to call Webservice A to authenticate to Webservice B and Webservice C, all with different endpoints. I know how to setup mocks to test B and C ...
19
votes
4answers
35k views

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

I have been struggling with this error for a few days now. I get this error every time I try to run a test for a method that has an httpCallout. I followed the architecture from http://www.salesforce....
17
votes
1answer
8k views

HttpCalloutMock Interface where there is more than 1 callout

Here is some background- I have an apex future method which is being called from an trigger. The future method then performs 3 different http calls to an external system (same endpoints). All requests ...
15
votes
3answers
6k views

Challenge with unit tests, mock callouts and running as a user

Background I am developing a number of business applications within a community. Many of these apps rely on callouts to another ERP system to verify or retrieve or push data. In order to adequately ...
13
votes
1answer
1k views

How do I unit test code that includes a callout?

This is a canonical question and answer developed by the community to help address common questions. If you've been directed here, or your question has been closed as a duplicate, please look through ...
11
votes
2answers
1k views

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

I'm attempting to do a unit test for a Queueable that does an Http callout. When I run the test I get an exception: "System.CalloutException: Callout loop not allowed". Previously I was using @...
10
votes
3answers
13k views

How to create mock class for multiple Callouts in single class

I am facing a issue with my mock class where I am suppose to create a mock class for a class that makes several callouts in a single method based on criteria's. Now , when I am trying to create a mock ...
10
votes
3answers
668 views

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

Seeing this error now if an object has an email alert on it during insert and using test.setMock Disabling the workflow/process builder that contains the email alert and the test passes fine. Test....
8
votes
3answers
7k views

how to test a future method which makes callouts?

I am practising the Integration workbook provided by salesforce. In tutorial#2 there's a class which makes future callouts to a Heroku app.This class is called by an after update trigger. Callout ...
8
votes
2answers
874 views

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

I'm building a Queueable that consumes an Azure service bus queue. Every call to the endpoint gives me one message so I keep on calling that same endpoint until I get a 204. The method that calls the ...
8
votes
1answer
365 views

Unable to implement HTTPMock from Managed package in Local Test Methods - Complete example with package

A while ago SF began failing tests that make callouts without an HTTP mock. When they did that we were unable to deploy some trigger as a managed package made a callout whenever an Opportunity was ...
7
votes
1answer
4k views

You have uncommitted work pending. Please commit or rollback before calling out

I have written a callout class as below.But when a account is created/updated am getting the exception like "You have uncommitted work pending. Please commit or rollback before calling out" at 36 line(...
7
votes
3answers
928 views

@testSetup method and System.CalloutException: You have uncommitted work pending

Apologies ahead of time for the lengthy post, but I'm receiving the following error when attempting to run the test method below. The error occurs when the upsertProject method is called. System....
7
votes
1answer
104 views

Combining DML, Callouts and Triggers gives CalloutException

When running unit tests, I'm currently getting: System.CalloutException: You have uncommitted work pending. Please commit or rollback before calling out The issue is that I have a trigger that ...
7
votes
1answer
129 views

How can I unit test a http callout that uses a certificate?

The .setClientCertificateName method of the HttpRequest class requires a string that is the name of a certificate in the organization. In a unit test class, is it possible to mock this method? When I ...
7
votes
0answers
615 views

How do you use a mock response when calling Auth.JWTBearerTokenExchange.getAccessToken()?

Let's say I have a class like this /** * Example of using JWTBearerTokenExchange to demonstrate issues with testing and mocks */ public class JwtBearerTokenExample { public static String ...
6
votes
2answers
3k views

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

I am trying to test an HttpCallout using mocks and I get the following error : System.TypeException: Supplied type is not an interface Stack Trace Class.System.Test.setMock: line 57, column 1 ...
6
votes
3answers
3k views

Method does not exist or incorrect signature in Test.setMock

when i try to save the following code, i got the error below. Following same instructions given in this link, but still errors. Am i doing anything wrong here? please suggest. http://www.salesforce....
6
votes
2answers
1k views

Getting CalloutException when testing Batchable class with Callouts and HttpCalloutMock

I'm trying to write test code for a Batchable class that AllowsCallouts. I've written a lot of other test code that uses HttpCalloutMock classes, and planned to do so for this class as well. And, ...
6
votes
1answer
4k views

how to get test coverage for @future and http response method in class

Below is my apex class and trigger, that is used to send sms from 3rd party application and that is working fine, but i'm not getting test coverage for class. If i use @future method my apex class ...
6
votes
2answers
414 views

Unit testing with callout conflicts with workflow rule

I am having a problem that I investigated down to this very simple minimalistic example that can be reproduced easily. 1) Create a workflow rule on the Account object that will send an email to the ...
6
votes
1answer
236 views

Unable to deploy httpCalloutMock based code to production due to NPE that does *NOT* occur in sandbox

Good morning. I've been beating my head against this for the past 24 hours. The following three links are: RestClient class - https://gist.github.com/2a2dd1e1b227dac24e80 RestClient Testclass - ...
6
votes
1answer
351 views

Async Callout Test Failing in Production

Situation I have implemented an API to communicate with the external system. After 18th of August test that i have on production have started failing showing the infamous: System.CalloutException: ...
5
votes
1answer
2k views

Mock Testing with Named Credentials

I've successfully implemented Named Credentials for a Basic Auth integration callout. However, when I went to write my test method, I noticed one minor annoyance. When I try to create a ...
5
votes
1answer
665 views

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

I noticed yesterday that one of my new test cases was failing, and determined from the logs that I was getting the beloved System.CalloutException: You have uncommitted work pending. Please commit or ...
4
votes
1answer
2k views

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

Actually there are many answers for this error "System.CalloutException: You have uncommitted work pending. Please commit or rollback before calling out", I tried all possible solutions, but couldn't ...
4
votes
1answer
116 views

How to confirm one of many HttpCalloutMock has been used

If I load multiple HttpCalloutMock's when settings up a test: Test.setMock(HttpCalloutMock.class, MockFactory.create()); And I am using a MultiRequestMock implementation: public without sharing ...
4
votes
1answer
5k views

HTTP callouts in Batch Apex

There are 1200 records/accounts in the 3rd party cloud based billing system. I need to update the records in SFDC. Now for each record, I need to do 2 callouts. I am thinking to write batch apex for ...
4
votes
1answer
407 views

How can I read the query string parameters in my Mock class?

My requirement is to call an external REST based service from a trigger. This service has been implemented with the GET method. I have finished the code for the trigger and the class. Now I am ...
4
votes
1answer
120 views

Unit test error - You have uncommitted work pending

I get an error in my apex test class You have uncommitted work pending I am aware of the Salesforce limitation in an http callout, we can't perform any DML operations before we send an http callout....
3
votes
4answers
390 views

Is it mandatory to write HttpCalloutMock?

I'm doing some service calls of which I do not have anything to do with the response. So it's most of like fire and forget thing. Below is my abstract implementation class A { public void Setup()...
3
votes
2answers
2k views

Test Unit: inserting test records before method with a callout?

I have a batch apex class that makes a callout at the beginning of each batch. I'm pretty certain that the callout error I'm getting isn't related to the order of DML statements and callouts within ...
3
votes
1answer
15k views

Calling an external REST service in apex

I need to make a call to an external xml-rpc service in apex. I have written this little apex code: public static void callPBA() { HttpRequest req = new HttpRequest(); HttpResponse res = new ...
3
votes
1answer
344 views

Testing future Callouts

I am trying to make a future callout sending email name etc from a trigger when a lead is created which is working. The problem is Testing. I am not sure how to go about making this work. I have been ...
3
votes
1answer
485 views

Create test Class for RESTFUL WEB SERVICE CALLOUT

Good day everyone, I am trying to create a restful web service callout everytime a contact in created,updated or deleted. I used this link as my reference http://blog.jeffdouglas.com/2009/03/16/...
3
votes
2answers
67 views

How to limit control flow during unit test?

I want to limit the control flow during a unit test so that it does not leave the method being tested. The unit test is for a controller method, and in that method the controller creates an object and ...
3
votes
2answers
1k views

Unit Test: System.CalloutException: You have uncommitted work pending. Please commit or rollback before calling out

BACKGROUND I am testing a method that results in two external calls out to an API. I have this Apex unit test: @isTest public static void createInvoice_Success(){ // arrange ...
3
votes
1answer
324 views

System.CalloutException: You have uncommitted work pending. Please commit or rollback before calling out in test class

I am facing this issue in a MockCallOut for http in test class. I know that a dml is not allowed just above the callout, so I have tried to put callout between Test.startTest() and Test.StopTest(); I ...
3
votes
1answer
212 views

My test classes are contributing towards uncovered code

I have a test class containing several WebServiceMock implementors. He looks like this: @isTest private class MyTestClass { public class ValidateMock implements WebServiceMock { public ...
3
votes
1answer
2k views

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

Firstly, I know what this message is trying to tell me: Methods defined as TestMethod do not support Web service callouts And I believe I have set up all the HttpCalloutMock's correctly, because ...
2
votes
1answer
701 views

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

We have method EnrollmentUtil.createNewEnrollment which contains a http callout on create of custom object Enrolment__c, and a test class to test this method. I'm trying to figure out how this class ...
2
votes
1answer
252 views

System.XmlException: start tag unexpected character =

This is the full error: System.XmlException: start tag unexpected character = (position: END_TAG seen ...<source> Bicycling</source><media:contenturl=... @1:688) So from my ...
2
votes
2answers
216 views

NO DML after Test.Start() but still getting System.CalloutException: You have uncommitted work pending

Basically, I'm getting the dreaded "System.CalloutException: You have uncommitted work pending." exception even though I'm 99.999% sure I'm not making any DML statements after running Test.startTest()....
2
votes
2answers
48 views

How to test a Platform Event Trigger that makes callout(s)?

I am integrating with a third-party API that calls an unauthenticated webhook (HTTP Post) that I have implemented with a small payload; my code then uses that payload to make an authenticated call to ...
2
votes
1answer
156 views

TEST : Http Callout Error

I was trying to test an apex class that uses some http callouts with HTTPMockRequest. When trying to run the Test class it gives me the following error: Methods defined as TestMethod do not ...
2
votes
1answer
2k views

Writing a test class for multi-callout method using MockHttpResponseGenerator

I have a future method which sends out two callouts. It looks something like this @future(callout = true) static webservice void SendCRMLeadFromTrigger(string leadID){ //FIRST CALLOUT ...
2
votes
0answers
162 views

How to test getContent() calls in Spring '17?

Now that the Apex Mocks API and its Stub API complement are available is it possible to properly test for a getContent() call? In the past I would just branch out if unit test was running and returned ...
2
votes
1answer
266 views

Testing HTTP Callouts Using zipped files in Static Resources

As per the documentation, we can use Mock.setStaticResource method to fake single webservice response in test class. StaticResourceCalloutMock mock = new StaticResourceCalloutMock(); mock....
2
votes
0answers
117 views

Using static resource in test class in order to test HTTP callout. Seems broken after update 1st of january [closed]

I'm using static resource in the code below to mock a httpcallout response. It seems like the update 1st of january did break the link - has anyone else experienced same issues? My question is then -...
2
votes
3answers
870 views

Sending attachment to JIRA From Salesforce Issue

I am trying to upload to JIRA from SF. I am using below code. HttpRequest req = new HttpRequest(); HttpResponse res = new HttpResponse(); Http http = new Http(); Blob headerValue = Blob....