Stack Exchange Network

Stack Exchange network consists of 174 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange

Questions tagged [httpcalloutmock]

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

9
votes
1answer
584 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 ...
0
votes
0answers
24 views

Get JSONException with valid JSON. How to figure out what's wrong?

I have a bit of JSON coming from a mock that needs to de deserialized into an object. The JSON itself is perfectly valid. It has been validated by multiple services, and they all say it looks legit. ...
0
votes
1answer
64 views

Is getContent not supported in unit tests, even if HttpCalloutMock is used?

I have created REST Service to return content from supplied image URL. In @post method of REST Service, I have used page.getContent() method to retrieve image content. My REST Service is working fine. ...
1
vote
1answer
87 views

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

We have a custom object called 'support' - we have process builder configured for this object. in one of the process builder action it will invoke Apex (Invocable Method). This will trigger a callout ...
0
votes
1answer
22 views

How to test 'throw new MyException()' in HttpResponse Callout?

I always get an Fail when try to test the negative scenario. How can I fix it? Sorry, I'm just learning. Callout: public class MyException extends Exception {} public class Token{ public String ...
0
votes
1answer
26 views

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

I try to use HttpCalloutMock to test my post callout. But I become System.NullPointerException: Attempt to de-reference a null object. I need to test and web token, and posted object. I'm just ...
-1
votes
1answer
73 views

How to fix Attempt to de-reference a null object in test for Post Callout?

I become an error System.NullPointerException: Attempt to de-reference a null object in my test: public class Token{ public String accessToken{get;set;} } public static String accessTokenBody(){ ...
1
vote
1answer
30 views

How can I test Post Callout with web token's access? HttpCalloutMock

My POST Method (data for web token - is my custom object's fields): public class Token{ public String accessToken{get;set;} } public static String accessTokenBody(){ //our web token (data is in ...
0
votes
1answer
44 views

How to test access token for HTTP callouts?

I have an access token for my HTTP callouts. How can I test it if I wanna use Static Resource? But I can also use the HttpCalloutMock Interface. it doesn't matter My access token is in custom object. ...
1
vote
1answer
40 views

How can I test Callouts with HttpCalloutMock?

How can I test this method? I try the test code from trailhead but can't make any coverage. Callout: public class Token{ public String accessToken{get;set;} } public static HttpResponse ...
0
votes
1answer
45 views

How to avoid: Error uncommitted work pending in my testclass

I am doing do a callout trough a processbuilder with a future call. Before I do the call I get some variables from a custom setting. When I do an insert in my testclass for my custom setting I get the ...
0
votes
1answer
182 views

Script-thrown exception in Test class for Batch apex callout

I have a batch class which makes multiple http callouts to an HR system and then with the absences returned checks for existing absences and upserts. However when I try and run test class for the ...
0
votes
2answers
28 views

Distance calculation API

Below is the code for distance calculation using matrix api. I have declared all the variables as private and called HTTP get method for this. in the log of the code I got an error Method does not ...
0
votes
1answer
30 views

Test Class for testing controller twice

I have a test class below that is successful and covers accessing my controller with a callout: @isTest static void testUpdateOfPack(){ MockPackageService mockRes = new MockPackageService(); ...
3
votes
1answer
277 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 ...
3
votes
2answers
462 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 ...
0
votes
2answers
163 views

Test class for Scheduled Batch class making a callout in the start method

I have two classes - BatchClass and SchedulerClass. Batch class makes an API callout in the start method and return the list of records to insert. This class is scheduled with the help of ...
-1
votes
1answer
26 views

How to create an external databate to test rest API classes

I have been doing some trailheads for REST api's I managed to create a callout class with a mock response based on a static resource, whitch gives me 100% coverage. I would like to see my Callout in ...
0
votes
1answer
65 views

Test failure 7% for HTTP Call out [closed]

I am not sure why I am getting the two errors I labeled below during the http send in the first class or in the Mock Impl class when I assert the endpoint. public class ProcessHandlerShowAccounts { ...
1
vote
2answers
197 views

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

I have almost tried all solutions available on internet but i still face the error. my Test class code is @isTest private class RegisterTest { @isTest static void existingUserCheck() { ...
0
votes
0answers
67 views

System.CalloutException: You have uncommitted work pending error when creating testing user and testing callout

There are a lot of questions related to "System.CalloutException: You have uncommitted work pending" errors, but could not find solution for following use case: Basically, a static method in the test ...
0
votes
1answer
75 views

Basic Mock Test Coverage Help

I think this will be a easy fix, I am sure it is something I am just overlooking at the end of the day. I created a mock class to test my callouts. It is called SingleRequestMockClass.cls, I got the ...
0
votes
2answers
62 views

Keep Failing Trailhead AnimalCallouts Tutorial

I am just copying and pasting an exercise in the Trailhead API Integration tutorial and the system.assertEquals(expectedValue,actualValue) line keeps failing between the mocked JSON I am feeding ...
1
vote
1answer
120 views

System.NullPointerException: Attempt to de-reference a null object test class

I have a class that makes a callout, it works fine, but the test class fails @isTest global class FinacleMockResponseGenerator2 implements WebServiceMock { global void doInvoke( Object ...
-1
votes
2answers
109 views

System.CalloutException: Unauthorized endpoint, please check Setup->Security->Remote site settings. endpoint [duplicate]

Try to do a mock callout internally on custom button click and also added the instance in remote site setting. @future(callout=true) webservice static void generatePDF(Id idList, String sessionId) { ...
0
votes
2answers
41 views

Can a Test Class have no member variables?

I am testing some HTTP callouts in Apex and instead of passing a newly created MyHttpCalloutMockImpl object in the test method every time, thought of declaring the same at class level and reusing it ...
-5
votes
1answer
405 views

Test class error for callout method, System.JSONException

I am only calling my method in Test Class and getting : System.JSONException: Invalid numeric value: Leading zeroes not allowed at input location [1,2] My callout method is: global static String ...
-4
votes
1answer
30 views

Callout Unit Test Need help in getting code coverage its getting to 46%

I have written a Callout Class Called RestCallout which makes Callout to an end point, get the httpResponse as xml and from the body of response gets different elements and updates the database. For ...
-2
votes
1answer
115 views

Getting error while writing test class

When i am trying to write a test class for my class (NameProjectTDfromAccolade).. I am getting the below error System.TypeException: Invalid conversion from runtime type List to Map on line ...
1
vote
1answer
578 views

HttpCalloutMock that returns a null response

I have a simple class that send an HTTPRequest and gets an HTTPResponse. I wanted to create a NullResponseMock that implements HttpCalloutMock and returns null, but I get an exception as: System....
3
votes
1answer
206 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
4answers
335 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()...
0
votes
1answer
73 views

facing System.CalloutException for HTTP response in test class

I'm facing "System.CalloutException: You have uncommitted work pending. Please commit or rollback before calling out" Here is my codes: (Adding only the relevant code) Callout Class which ...
0
votes
2answers
393 views

HTTP Callout Test Error

I'm trying to push some code from one of our sandboxes to production. Class: public class CloudtoDevOps { @future (callout=true) public static void sendNotification(String ClientID, ...
0
votes
1answer
527 views

Test class for MetadataService

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 ...
1
vote
1answer
351 views

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

createFolderResponse = SharePoint_IntegrationHandler.createFolder(null,parentFolder); if(createFolderResponse.getStatusCode() == 200 || createFolderResponse.getStatusCode() == 201){ //----------...
0
votes
1answer
272 views

test class for the @auraenabled method that only call on button clicks

I am facing issue in writing test class that has 1 method which only executed when the lightning buttons get clicked.please suggest on this issue. Any help would be appreciated.
0
votes
2answers
188 views

Include HttpCalloutMock in TestSetup

I am writing a testclass that has to perform a callout. The callout returns a JSON string that is used to update a custom object in salesforce. This custom object has a master-detail relationship ...
0
votes
0answers
395 views

Callout in test method not returning mock response

I am trying to write a test class for testing an HTTP callout. Test class compiles without errors. However, I am not getting the mock response inside. Below is the relevant parts of my code. ...
5
votes
2answers
316 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 ...
1
vote
0answers
185 views

Testing future callout method

I am attempting to test a @future callout class that pings another class to make an API call and update some fields in the Lead object. I am using the mock http method and here's the script: @isTest ...
1
vote
0answers
64 views

create a mock class for callout in managed package [duplicate]

we have a managed package and the function call_out is making a http request. In the package we can use this code to create a test for it: Test.startTest(); Test.setMock(HttpCalloutMock.class,...
1
vote
1answer
208 views

Visual Workflow plugin to callout to an external webservice and POST JSON

Can anyone help in providing an example of an APEX class which can make a HTTP JSON POST request to an external webservice using a plugin for SF visual workflow? Example I tried following here ...
5
votes
0answers
321 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 ...
1
vote
1answer
49 views

Error in Httpcallout test class for contact object

I have written test class for my http future method I am receiving assertion error. Please look at my class & test class below. public with sharing class contactAutoAddress { private static ...
0
votes
1answer
87 views

Error in Http test class

All I have written test class for my http future method I am receiving assertion error. Please look at my class & test class below. public with sharing class contactAutoAddress { private static ...
1
vote
3answers
1k views

Mock Service .setBody()

I am super stuck on my test coverage. I built a generic mock test class so I can reuse it. I typically just plug in the status code, status string, and body to be returned. I have used this mock class ...
1
vote
0answers
27 views

Test Webservice Callout [duplicate]

Can anyone see what I am doing wrong? I am not getting back any data from my mock callout. Below- jsonStr is what is returned from the webservice and it is being returned as null. Apex: public ...
8
votes
1answer
216 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 ...
0
votes
0answers
186 views

Test class code coverage for catch block in httprest api

below is my class code. code is not getting covered for the catch block. current code coverage is 75%. Also given below is my test class code. In need at least 90%. public class ...