Questions tagged [httpcalloutmock]

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

Filter by
Sorted by
Tagged with
0
votes
0answers
15 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
22 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
652 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
67 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 ...
0
votes
0answers
10 views

I need help for Test Apex class [duplicate]

I got 52% with this test code, Please give me some advice to improve the percentage for passing. @isTest global class BLmainMock implements HttpCalloutMock { global HTTPResponse respond(...
0
votes
0answers
23 views

How to mock multiple post callouts in test - different responses same endpoint [duplicate]

I have some code that is potentially making multiple post requests to the same endpoint within one transaction, then updating records based on the response. I think the code works, but I want to ...
5
votes
0answers
62 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
34 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
31 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
244 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
35 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
29 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
32 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
113 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
225 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
89 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
157 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
37 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
29 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
113 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
1k 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
79 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
389 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
149 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
268 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
135 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
169 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
35 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
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 ...
0
votes
0answers
37 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
323 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
379 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
24 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
39 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
997 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
619 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
494 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
347 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
2k 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
173 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
52 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
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 ...
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 ...
0
votes
2answers
779 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
30 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
150 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
2k 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() { ...
2
votes
1answer
144 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
213 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 ...