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
1 answer
21 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 ...
0 votes
1 answer
66 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 ...
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 ...
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 ...
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-...
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-...
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 };...
0 votes
1 answer
260 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,...
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 ...
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 ...
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 ...
0 votes
0 answers
168 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 ...
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....
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....
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 ...
0 votes
1 answer
958 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 ...
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 ...
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 ...
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'; ...
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/...
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 ...
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 ...
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 '@...
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 ...
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 (...
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 : ...
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(); ...
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"...
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 ...
0 votes
0 answers
72 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 ...
0 votes
0 answers
69 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 ...
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 ...
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 ...
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 ...
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 ?
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; ...
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,...
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 ...
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 ...
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 () { ...
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(...
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 ...
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 ...
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, ...
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 ...
0 votes
1 answer
1k 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 ...
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 ...
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 ...
0 votes
1 answer
804 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 ...
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 ...