Questions tagged [mock]

Mock classes are used in testing to simulate the responses of real code in a controlled, reproducible fashion. In particular, HTTPCalloutMock and WebServiceMock classes are used to supply response values to callouts made during a unit test.

Filter by
Sorted by
Tagged with
17 votes
2 answers
7k views

How to mock a Database.saveresult?

We need to use database.insertImmediate(). It returns List<Database.SaveResult>. Now, we want to mock the database.insertImmediate() and not do the dml call and need to be able mock the ...
Jorjani's user avatar
  • 2,192
11 votes
3 answers
6k views

LWC + Jest: Mocking imperative Apex and returning a value

I'm going to do a Q&A-style entry here, since I couldn't find anything on the web describing this use case. I have seen several threads here describing how to do an empty mock on an Apex call in ...
Matthew Souther's user avatar
9 votes
2 answers
3k views

Is there a way I can mock an AggregateResult?

I'm trying to write most of my tests without hitting salesforce database. For that I'm implementing some simple mock class where I just return some SOQL queries when needed. The problem I'm facing now ...
Francisco Riccomagno's user avatar
9 votes
1 answer
411 views

ApexMocks - Assertion Fails

I am hoping for some help in figuring out why some mocking is not working. I am currently using ApexMocks and the FinancialForce Apex Common Library and knowledge of those would be necessary to help ...
techbusinessman's user avatar
7 votes
2 answers
1k views

Messaging.SendEmailResult Type cannot be constructed?

I'm trying to create a simple mock of messaging Messaging.sendEmail and in my code I'm trying to do the following to construct mocked instances of Messaging.SendEmailResult Messaging.SendEmailResult ...
Michael Paler's user avatar
6 votes
1 answer
2k views

Test.createStub for class which returns Batchable in method

I believe I've found a bug, but I wanted to throw it to the community to see if I'm not understanding something. I'm implementing a test class and using Test.createStub to mock a dependency. The ...
Cappielung's user avatar
5 votes
1 answer
2k views

How to mock navigator.geolocation in a LWC?

Having a component that uses the embedded geolocation feature to get coordinates, how can I mock the call in a Jest test? The component's code work in the org: ... get locationService () { ...
Renato Oliveira's user avatar
5 votes
1 answer
861 views

Mocking SObjectType to make tests independent of 3rd party managed packages

When unit testing code that interacts with SObjects in other managed packages, being able to mock those SObjects would be helpful so the packages don't have to be installed to run the unit tests. The ...
Keith C's user avatar
  • 135k
4 votes
2 answers
206 views

Param is unnamed__0 if StubProvider used on interface (instead class)

I am unable to use the System.StubProvider to mock/stub a complicated Backend class of which I have no implementation but only an interface public interface ShopBackend { void initialize(); ...
Robert Sösemann's user avatar
4 votes
1 answer
315 views

Is there a way to fake a method response using Mock or Stubs?

I have a helper method in my code called canQueryObject, and that method returns a boolean. I have some code that calls the method via the following line of code: if(!myObjectHelper.canQueryObject(...
WEFX's user avatar
  • 259
4 votes
1 answer
2k views

test.setMock issue passing a parameter

I'm having difficulty passing a paramater to a HttpCalloutMock class. I have a trigger that calls a future callout on insert and on the response I'm receiving the record id so I can do the relevant ...
JPG's user avatar
  • 198
4 votes
1 answer
189 views

How to mock App Exchange package's API response?

I am using an app exchange package in my code, they have exposed a few global methods that I can use to get my job done. It all works beautifully. The issue comes with testing. I want to test ...
Pranay Jaiswal's user avatar
3 votes
1 answer
2k views

Unit Testing External Object with Mocked Data

New to the SF dev world so I am struggling a bit with setting up some unit tests on external objects. I have found very little info on this, most dating back a few years. I did find a post here on SO ...
SBB's user avatar
  • 173
3 votes
0 answers
2k views

LWC - Jest test for Apex calls in wired function

We have a class that calls the getRecord uiAPI and then using the result makes an APEX call to get more details from another custom object. import { LightningElement, api, wire, track } from 'lwc'; ...
raj's user avatar
  • 183
3 votes
0 answers
413 views

Mock a class that implements Schedulable using Stub API

I'm using Stub API (https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing_stub_api.htm) to mock a class that implements Schedulable and because of this I'm getting an ...
ashkanent's user avatar
  • 335
3 votes
1 answer
231 views

ApexMock FileNotFound Exception

I am trying to learn how to write tests for apex services using ApexMocks framework. I want to generate Mock classes through apex-mocks-generator and I am following simple instructions given in this ...
Faiza Iqbal's user avatar
2 votes
1 answer
4k views

How to mock custom LWC components in JEST

I am still new to JavaScript and especially Jest, so apologies in advance for the maybe stupid and uninformed question. To isolate my tests, I want to mock one of my custom table components. In short,...
J. Schreiber's user avatar
  • 1,520
2 votes
1 answer
739 views

Mocking Datacloud.FindDuplicates.findDuplicates(leads) for Integration test

Would be great if someone could point me in the right direction here. I have this static method as follows... public static void createDupeMap(Lead[] leads) { CheckExistingRecords cer = new ...
edralph's user avatar
  • 552
2 votes
1 answer
1k views

How do I mock a child LWC in a parent's Jest Unit Test?

I have two components that are in my app: parentComponent childComponent. 'parentComponent' includes 'childComponent' as a component. E.g: parentComponent.html <template> <c-child-...
Rob Baillie's user avatar
2 votes
1 answer
99 views

Webservice mock callout goes to the webservice original class and fails

I am using a WebService callout, returning a string, and testing it; I am quite new to it. The problem is in the Mock class. When I am debugging it, the response element is created, the string is ...
Franktrt's user avatar
  • 173
2 votes
1 answer
414 views

LWC jest mock custom permission: jest.mock( "@salesforce/customPermission/my-custom-permission",

I am mocking custom permission successfully like this: jest.mock( "@salesforce/customPermission/my-custom-permission", () => { return { default: true };...
Mira's user avatar
  • 21
2 votes
1 answer
526 views

fflib_ApexMocks is not returning the expected value

I am using fflib_ApexMocks without any issue till today. However, for this case, I am not able to understand what I am doing wrongly. This is the magical mock: mocks.when(appointmentServiceMock....
lopez.regalado.fj's user avatar
2 votes
1 answer
439 views

Can we overwrite mocked values returned in LWC Jest?

I am mocking the values in a custom label using this from https://developer.salesforce.com/docs/component-library/documentation/en/lwc/lwc.unit_testing_using_jest_patterns import myImport from '@...
sfdxOlive's user avatar
2 votes
1 answer
130 views

Is there any way to mock and test Salesforce Streaming?

Is there any way to mock and test Salesforce Streaming? How do you test Salesforce Streaming API ?
a3.14_Infinity's user avatar
2 votes
1 answer
643 views

mocking method with any instance, string and enum

I am trying to mock a class with a method that has 3 arguments, and these 3 arguments could be any of its own types. I used the fflib_ApexMocks to return 'hello world' whenever the method is called; ...
Cuban coffee's user avatar
  • 1,532
2 votes
1 answer
225 views

Mocked imperative Apex method does not return mocked value

My lwc has an input and a button. When I press the button an apex call with the input's value is made. Even though the method is called with the right params, the return value is some sort of empty ...
Christian Llanos's user avatar
2 votes
0 answers
742 views

With LWC Jest, how to mock custom components

I'm trying to unit test my LWC which has another custom component as child. I tried to use the steps describe in this post : import { createElement } from 'lwc'; import EfxProjectReport from 'c/...
Ombrelin's user avatar
  • 403
2 votes
0 answers
316 views

Post callout behavior assertions for unit test with static resource callout mock fail

I have a working queueable callout, and working tests using static resource callout mock I can successfuly run basic assertions on the mock response body and status. However, the callout method in ...
gorav's user avatar
  • 4,485
1 vote
2 answers
777 views

What is the use of mock callouts in Apex?

What is the use of mock callouts in Apex? They are used for testing purposes, because it is impossible to perform a callout in method annotated with @isTest. And because it is necessary to have test ...
iloveseven's user avatar
  • 1,105
1 vote
2 answers
7k 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 ...
user69176's user avatar
1 vote
3 answers
5k 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 ...
Olivia's user avatar
  • 3,431
1 vote
1 answer
2k views

Can we mock relationships in Apex?

I have a custom object called Document__c that looks up to Account. Account has a lookup field to another custom object called Business__c. In SOQL, the following query will return the id of ...
Jorjani's user avatar
  • 2,192
1 vote
1 answer
190 views

Why Test.setMock() is not working in case of static resource?

Test.setMock() is not working Error: Method does not exist or incorrect signature: void setMock(System.Type, System.StaticResourceCalloutMock) from the type test Source Code: @isTest private class ...
ZeroX's user avatar
  • 21
1 vote
2 answers
2k views

HttpRequest and HttpResponse is not covering in test class

I wrote a Testclass(Mock response) by implementing HttpCalloutMock interface to cover HttpRequest in class, I given some fake values to it also. But while calling the class, its not covering in ...
KS Kumaar's user avatar
  • 753
1 vote
1 answer
907 views

Stub api with static methods

I have a global class with a static method which makes post callout and I want to use Stub API, but I don't get how to do it when a method is static. I understand what to do inside mock classes, but I ...
lemleborze's user avatar
1 vote
1 answer
1k views

You have uncommitted work pending. Please commit or rollback after [sic] calling out

Below class gives error "You have uncommitted work pending. Please commit or rollback after calling out". //Call out from batch public void execute(Database.BatchableContext bc, List<...
sf_user's user avatar
  • 2,208
1 vote
1 answer
2k views

how to mock Object list in a test class?

This is a method in my apex controller class: @AuraEnabled public static void sendMail(List<Object> selectedRows) { if (selectedRows.size() > 0) { for (Object obj : ...
Jayesh Babu's user avatar
1 vote
1 answer
2k views

In an unit test for an LWC, how can I mock out a fetch to a third party API?

I have a Lightning Web Component with the following HTML: <template> <h1>Current price of a bitcoin in Euro</h1> <lightning-formatted-number currency-code="EUR"...
Brian Kessler's user avatar
1 vote
3 answers
1k views

Testing Multiple Mocks for Different Test Methods

I am trying to get my code coverage up and I have a class where I am getting responses from an endpoint. I need to get different responses for different tests. Ex: Test Method 1 I need to simulate ...
S.B.'s user avatar
  • 2,039
1 vote
1 answer
176 views

Adding open-source libraries to a managed package

We want to be able to level-up our testing and have found ApexMocks as a very helpful library that we can use for this purpose. I'm not sure if I can cover ALL the use cases but I hope it can help us ...
Jorjani's user avatar
  • 2,192
1 vote
1 answer
312 views

How to mock, inject or otherwise make Custom Metadata available to apex tests

I have some code which relies on some Custom Metadata records to exist, but I don't want my tests to depend on real records in the org. How can I either inject or mock custom metadata records into ...
ministe's user avatar
  • 324
1 vote
1 answer
492 views

Mocking Object list issue

This question is sort of a continuation to this question. I have this method: public static void sendMail(List<Object> selectedRows) { if (selectedRows.size() > 0) { for (...
Jayesh Babu's user avatar
1 vote
1 answer
172 views

Problems with callouts Mock and tests

We have a problem in our organization and I was hoping for some advice from you on how to best tackle this. We have a handler class that makes a callout to a Master Data Management system for account ...
Ivar Gunnarsson's user avatar
1 vote
1 answer
3k views

Mock class not working with Dynamic & Unique end-point

i am fetching some remote data into my salesforce and i am using "oauth_signature" in my end-Point URL for remote web services so my API URL is unique for every API request. my all functionality is ...
Rajinder's user avatar
  • 119
1 vote
0 answers
45 views

Stub API not calling trigger

I am using Apex DML mocking using stub api and got a reference from multiple blogs, https://github.com/packocz/Pragmatic-Bear-Blog https://github.com/Coding-With-The-Force/Salesforce-Development-...
sumit's user avatar
  • 479
1 vote
0 answers
1k views

Proper way to mock svg html template in Jest?

I want to create lwc component which selectively renders svg from html template files. I wonder how to mock that templates for the needs of JEST test. Import the same templates for component and test ...
Grzegorz Połuch's user avatar
1 vote
0 answers
323 views

Is it possible to mock an approval process through Apex?

I have a generic piece of code that is responsible of approve a record. It does not assume the SObject type nor the process definition. For example, something like the following: public static Boolean ...
gvgramazio's user avatar
0 votes
1 answer
533 views

Is Test.isRunningTest() needed to prohibit an apex test from performing a callout even if you use Test.setMock()?

Taking into consideration the below pseudo code, do we need to check if an apex test is running before performing callout? Or does the Test.setMock() method automatically prevent callouts during a ...
parkerbbrown's user avatar
0 votes
2 answers
164 views

Mocking BusinessHours records returns System.MathException: null

I am stuck on a test method and any help would be highly appreciated. To provide some context: I created a method that queries a specific BusinessHours record on the DB and validates if a Case was ...
user1067017's user avatar
0 votes
1 answer
217 views

Malformed JSON: Expected '[': wrong test, callout or mock?

I have System.JSONException: Malformed JSON: Expected '[' at the beginning of List/Set and I don't know how to fix it. Callout: public class Callout { public List<Tool__c> toolsList; public ...
user60684's user avatar
  • 163