Questions tagged [httpcalloutmock]

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

Filter by
Sorted by
Tagged with
-1
votes
1answer
20 views

how to test the Rest Callout using Stub API

Hi Greetings for the day. using the below code ,I have test the Rest callouts by using Stub API .Test class has been completed with out failure but received code coverage Zero.how to test the Rest ...
0
votes
0answers
26 views

Document.load() fails: System.XmlException: Failed to parse XML due to: only whitespace content allowed before start tag and not {

I am getting the following error when tying to use the Document.load() method for my xml: System.XmlException: Failed to parse XML due to: only whitespace content allowed before start tag and not { (...
0
votes
0answers
48 views

Test Class of batch Apex which includes a Http Callout in the execute method

I'm stuck trying to create a Test class for my batch which execute a Http request. I tried with Test.setMock and also inserted "!Test.isRunningTest()" in the batch but nothing changes. I ...
2
votes
1answer
72 views

Test Class with HttpMockCallout is throwing error

I am trying todo the Super Badge for the Data Integration Specialist. I wrote the Apexcall that will connect to the external system and send out the opportunity information. When trying to write the ...
1
vote
1answer
56 views

Testing a class that has multiple callouts?

What is the best practice for writing apex test classes where you have multiple callouts? My Test Class would then be testing the secondCallout method which happens to also call a firstCallout method ...
0
votes
2answers
43 views

Test Coverage for a HttpCalloutMock?

We have a test class, and it has @isTest and for some reason, salesforce is showing this class as needing coverage? Shouldn't this class be excluded from code coverage? @isTest(seeAllData=false) ...
0
votes
0answers
38 views

Problem with Apex HTTP callout POST Test [duplicate]

I get the following error: "System.CalloutException: You have uncommitted work pending. Please commit or rollback before calling out" when executing my test. The code coverage is already at ...
0
votes
1answer
38 views

Problem with testing void HttpRequest

I can only get 23% code coverage with what I have. The http request in the method is not getting covered for some reason.. Here's what I've tried: Class: public class PostClientes { @...
5
votes
1answer
538 views

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

In My Method making two callouts in a single transaction Get Access Token For that API --> GetAccessToken Execute Payment When i ran the test class getting following error 08:50:00:839 ...
0
votes
0answers
16 views

Test Class for mulitple Callouts [duplicate]

I have the apex class that makes the call to two endpoints in the same method like below @future (callout=true) public static void createSegSubSegment(String jsonBody,String endPoint) { String ...
0
votes
1answer
132 views

Test Class for the Apex Callout with OAuth

I am trying to write the Test Class for the Apex Callout which uses the OAuth. The Apex Class with the future method is like below @future (callout=true) public static void createSegSubSegment(String ...
14
votes
2answers
771 views

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

We have 100+ tests failing now that our main testing sandbox has received the 2020 release. We can no longer deploy. We're getting many errors like Unexpected error: You have uncommitted work ...
0
votes
0answers
910 views

Testing Asynchronous Callouts - order of execution of scheduled job - “Apex Specialist” superbadge

While trying out the Superbadge challenge - 6 "Test scheduling logic" https://trailhead.salesforce.com/en/content/learn/superbadges/superbadge_apex , I want to assert on the quantity of the product in ...
5
votes
0answers
123 views

Test fails with runAs, queueable and callout in specific org

Has anyone got an idea what could cause the following test to fail in one org and pass in another? The error is System.CalloutException: You have uncommitted work pending. Please commit or rollback ...
0
votes
0answers
46 views

You have uncommitted work pending - apex test with three callouts

I am trying to create a test where there are three (mocked) callouts to a web service which then creates or updates records. Within my test class if I try to run the callout/update code twice within ...
-1
votes
1answer
49 views

Test class for Http Request code coverage is 50 percent [duplicate]

I have a class public class myclass { @AuraEnabled public static List <Json2Apex> customerData(String startDate, String enddate){ List <Json2Apex> LSData = new List &...
2
votes
2answers
984 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 ...
1
vote
0answers
39 views

'uncommitted work pending' callout exception thrown in *tests only*

I am converting a lead in the Execute method of a batch class, then making 2 callouts with those results in the Finish() method. It works gloriously and throws no errors. However, in the test ...
1
vote
0answers
32 views

Correctly Write Test and Callout test on trigger [duplicate]

I have a trigger on Account after insert and after update. In both cases I need to call my BUS to update in the compagny database. Since I will have to deploy my dev into QA I need to write a test ...
0
votes
0answers
46 views

Test.setMock working in Scratch org but not in sandbox Org

We have batch class used to make an API callout and doing Opportunity upsert DML operation in the Start method. Based on the opportunity's return value, we are making another API callout and doing ...
0
votes
2answers
185 views

mock test class and test class for 2 classes

I have 2 class A and B. A has code for Http Callout and from B we are calling A's class method which has callout code. So should we write mock test class for A only or do we have to write mock test ...
7
votes
1answer
436 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 ...
0
votes
1answer
173 views

Rest Callout Apex Test: How to Test @AuraEnabled Method?

guys! I have a problem with one method in my Apex class, which I need to cover by tests. This class uses "GET" callout and checks, if the input field is empty (null). If it is, the @AuraEnabled ...
0
votes
1answer
343 views

Rest Callout Test Class : How to Cover @AuraEnabled method?

I'm new to Salesforce and now I'm struggling with one task. I need to make test class, but I'm wondering: how do you cover the second @auraEnabled method with If statement? Here is the code: Apex ...
1
vote
1answer
38 views

Can I query custom metadata type record in a HttpCalloutMock?

I am trying to understand if I can make use of Custom Metadata Type record by quering it in a HttpCalloutMock this way: public HTTPResponse respond(HTTPRequest request) { String myString = [Select ...
0
votes
1answer
31 views

Simplifying REST API calls

I'm new to Java programming and I'm trying to reduce the number of REST API calls I make in my program to better organize it and I'm not sure how best to approach it if the simplest way is to have a ...
0
votes
1answer
327 views

How can I return HttpResponse after calling a class which implements Queueable

As described in the title, I have a class which implements the Queueable interface, for each item it sends an http request and I would like to get the response back and use it. My problem is that I ...
1
vote
2answers
3k views

System.TypeException: Mock object doesn't implement the supplied interface. Tried to add different methods in the mock class

I created a mock class which has different methods WHen I tried to get the different response based on my scenarios. Im getting the error like 'System.TypeException: Mock object doesn't implement ...
0
votes
1answer
124 views

How to mock test a rest API implementation

I have a method public static Map<String, Object> createNewAccessToken() { Map<String, Object> access_token = null; HttpRequest req = new HttpRequest(); req....
2
votes
1answer
629 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 ...
0
votes
0answers
274 views

Mock HTTP Call Out In Test to Return XML

I am very new to HTTP Callouts and now I am in the testing phase. My HTTP Callout returns XML This is the link. I input a company already, soulcycle, in case anybody wanted to see what was being ...
1
vote
1answer
547 views

Test error condition in finish() method of Batchable class

Trying to figure out how to test error condition in the finish() method of this Batchable class. I am testing the error condition in the execute() method by calling it separately in the test and ...
1
vote
1answer
226 views

Test error condition in batch class with callouts

I have an Apex class with a callout (oregonDistricts.apxc) and I have two tests written that test both the success and error conditions. These tests are passing and the class has 100% code coverage. ...
4
votes
1answer
311 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 ...
0
votes
0answers
36 views

Apex Testing HttpCalloutMock failing

The method is for testing a class that use a soap envelope to check if an address exists and if it does return true, otherwise it returns false. It implements Jennifer Symonds xpath to verify the ...
13
votes
1answer
2k 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
39 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. ...
1
vote
1answer
483 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
26 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
40 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
2k 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
898 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
931 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
675 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
2k 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
4k 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
243 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
89 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
4k 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
2k 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 ...