Skip to main content

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
0 votes
0 answers
145 views

How to use Apex Mockery Lib for simple scenario?

Was wanting to use Stub API for mocking test for some time but had been too busy to try it out. I saw some devs recommending FFLib but I find it too heavy a framework for most SF envs. So looking for ...
compski's user avatar
  • 1,352
0 votes
1 answer
200 views

LWC Jest: test a component that uses lightning/toast

The lightning/toast utility is GA as of Winter '24, but I haven't seen anything on StackExchange or in the docs about how to write tests for components that use it. One you import it into a component ...
Matthew Souther's user avatar
0 votes
1 answer
216 views

How to mock AuthSession in test class

I want to cover this method : @AuraEnabled public static void endUserSession(Id userId){ List<AuthSession> uSessions = [ select Id , UsersId FROM AuthSession WHERE UsersId =:userId AND ...
Fark's user avatar
  • 27
1 vote
1 answer
1k 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
0 answers
92 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
3 votes
1 answer
2k 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
0 votes
1 answer
486 views

How to mock a file attachment size in Apex Salesforce to test

I have to test a method that return a boolean false when the attachment upload is bigger than 25 megas, I tried to test this one using the code below but there is a limit dml exception string too long,...
GRC7's user avatar
  • 3
1 vote
1 answer
359 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
2 votes
1 answer
342 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
1 answer
580 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
0 votes
1 answer
939 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
0 answers
198 views

How to generate key prefix for "unstable" types

I use a mocking framework that exposes a method for generating mock record ids. The utility method uses getKeyPrefix() to mock an Id. This works for most object types but doesn't work for __Share ...
TemporaryFix's user avatar
  • 2,946
0 votes
0 answers
29 views

Testing a Callable via ApexMocks [duplicate]

I have a trigger that calls a Callable class when a status is changed. I want to verify that the call function is called with the correct argument and so I tried mocking Callable using fflib_ApexMocks....
Katherine M's user avatar
2 votes
1 answer
690 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
0 votes
2 answers
215 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
  • 1,017
0 votes
1 answer
1k views

LWC jest mocking '@salesforce/apex/refreshApex' problem

I'm trying to get my arms around testing LWC. I'm trying to test a button click that calls some Apex, then calls 'refreshApex' and then throws a success toast. It seems I'm mocking my own custom Apex ...
RonApex's user avatar
  • 75
1 vote
1 answer
1k 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
2 votes
0 answers
780 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
  • 413
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
415 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
2 votes
1 answer
486 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
12 votes
3 answers
7k 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
2 votes
1 answer
882 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
  • 562
1 vote
1 answer
573 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
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
4 votes
2 answers
216 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
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
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
0 votes
0 answers
74 views

Mock Webservice called from a trigger repeats until I get a 'too many callouts: 101' message

I'd really appreciate any suggestions that the group may have. I'm writing a test class for a SalesForce trigger that calls a webservice. The trigger works fine in the Sandbox environment, but ...
djpackman's user avatar
0 votes
0 answers
74 views

Getting INSUFFICIENT_ACCESS_ON_CROSS_REFERENCE_ENTITY when mocking an Object using Stub API

I am mocking a routing rule object by using the JSON list below that has an ID of type Routing_Rule__c. The Test is about creating some rules and setting some flags that would cause the lead object to ...
kratos's user avatar
  • 541
5 votes
1 answer
969 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
  • 136k
3 votes
1 answer
3k 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
2 votes
1 answer
136 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
789 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,596
2 votes
1 answer
5k 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,579
4 votes
1 answer
225 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
1 vote
2 answers
8k 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
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
4 votes
1 answer
334 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
0 votes
1 answer
237 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
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
0 votes
1 answer
1k views

Testing/Mocking design pattern to test Messaging.SendEmailResult

I have some code in an After Insert trigger that does some conditional DML updates to the records being inserted if a mail is successfully sent out. I'm looking for a solid, modular way to test this, ...
Michael Paler's user avatar
1 vote
2 answers
882 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,115
6 votes
1 answer
3k 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
0 votes
1 answer
2k views

Pass a List with a custom object type as a parameter

I am trying to increase my code coverage, I have only provided parts of my code that I am trying to cover. I have to create a List to be able to pass over my values into the method. My Errors I have ...
nameishi'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,111
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,222
0 votes
1 answer
823 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 ...
Olivia's user avatar
  • 3,471
1 vote
1 answer
195 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,222
3 votes
0 answers
451 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