Questions tagged [httpcalloutmock]

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

Filter by
Sorted by
Tagged with
0 votes
1 answer
23 views

How to set StatusCode on Test.setMock [duplicate]

I have a simple class that just calls the generic HTTPCallOUt method for its test class. I want it to be able to fail in order to catch all the code. Here is my code: Test.startTest(); Test....
user avatar
0 votes
1 answer
28 views

Test class for HTTPCallout POST with Invocable Method from Flow [duplicate]

I'm still pretty new to this, and I've looked around and haven't been able to find the solution that combines everything like I'm trying to do. I've tried quite a few things, but here's where I ...
user avatar
0 votes
1 answer
69 views

Test Class Error - System.QueryException: List has no rows for assignment to SObject json | Cover JsonGenerator in Test Class

"Hello everyone" I have a week with this problem in the testClass. So the problem is the following: System.QueryException: List has no rows for assignment to SObject json. The problem comes ...
user avatar
1 vote
1 answer
132 views

Constructor chaining giving method not defined

I have the following class with constructor chaining , but the validations are returning Method not found or incorrect signature class @IsTest global class CalloutMock implements HttpCalloutMock { ...
user avatar
0 votes
0 answers
19 views

Need to write a test class for Http post class [duplicate]

@RestResource(urlMapping='/PioneerRxChangeEventListner/*') global without sharing class PioneerIntPharmacyApiClass{ @HttpPost global static ParentResponseWrapper doPost() { // variable ...
user avatar
0 votes
1 answer
399 views

The Test.isRunningTest() function is not working

I am using if(Test.isRunningTest()) condition to set the response code to 200 for code coverage related to my HTTP callout class, so that the success scenario gets covered. But in the coverage details ...
user avatar
0 votes
1 answer
153 views

Test class for queueable apex trigger handler with http callout

Whenever a new record of object abc is created, a queueable trigger handler makes http callout. My trigger handler is working fine and i am able to do what I intended, but when I test this trigger ...
user avatar
  • 25
-1 votes
1 answer
175 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 ...
user avatar
0 votes
0 answers
426 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 { (...
user avatar
  • 305
0 votes
0 answers
267 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 ...
user avatar
2 votes
1 answer
324 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 ...
user avatar
  • 599
1 vote
1 answer
1k 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 ...
user avatar
  • 372
0 votes
2 answers
96 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) ...
user avatar
  • 372
0 votes
0 answers
40 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 ...
user avatar
0 votes
1 answer
44 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 { @...
user avatar
6 votes
1 answer
1k 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 ...
user avatar
0 votes
0 answers
17 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 ...
user avatar
  • 599
0 votes
1 answer
626 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 ...
user avatar
  • 599
14 votes
2 answers
828 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 ...
user avatar
  • 1,159
0 votes
0 answers
2k 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 ...
user avatar
5 votes
0 answers
162 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 ...
user avatar
  • 2,499
0 votes
0 answers
50 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 ...
user avatar
  • 888
-1 votes
1 answer
103 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 &...
user avatar
  • 55
2 votes
2 answers
2k 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 ...
user avatar
1 vote
0 answers
43 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 ...
user avatar
  • 142
1 vote
0 answers
33 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 ...
user avatar
  • 21
0 votes
0 answers
64 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 ...
user avatar
  • 392
0 votes
2 answers
278 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 ...
user avatar
  • 37
7 votes
1 answer
826 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 ...
user avatar
0 votes
1 answer
255 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 ...
user avatar
0 votes
1 answer
460 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 ...
user avatar
1 vote
1 answer
54 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 ...
user avatar
0 votes
1 answer
43 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 ...
user avatar
0 votes
1 answer
697 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 ...
user avatar
  • 1,443
1 vote
2 answers
5k 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 ...
user avatar
0 votes
1 answer
293 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....
user avatar
2 votes
1 answer
962 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 ...
user avatar
0 votes
0 answers
457 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 ...
user avatar
1 vote
1 answer
845 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 ...
user avatar
  • 237
1 vote
1 answer
433 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. ...
user avatar
  • 237
4 votes
1 answer
470 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 ...
user avatar
  • 8,262
0 votes
0 answers
40 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 ...
user avatar
  • 147
14 votes
1 answer
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
0 answers
48 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. ...
user avatar
1 vote
1 answer
598 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 ...
user avatar
0 votes
1 answer
33 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 ...
user avatar
0 votes
1 answer
62 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 ...
user avatar
  • 133
-1 votes
1 answer
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(){ ...
user avatar
  • 133
1 vote
1 answer
1k 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 ...
user avatar
  • 133
0 votes
1 answer
1k 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. ...
user avatar