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
1
vote
1answer
86 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 ...
1
vote
0answers
120 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
0answers
160 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
0answers
63 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
1answer
165 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 '@...
3
votes
2answers
1k 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
1answer
190 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
1answer
87 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
1answer
359 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
2answers
151 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
1answer
861 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"...
3
votes
0answers
926 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'; ...
0
votes
0answers
28 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
0answers
48 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
1answer
415 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
1answer
847 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 ...
2
votes
1answer
66 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
1answer
153 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
1answer
2k 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,...
4
votes
1answer
79 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 ...
1
vote
2answers
3k 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 ...
5
votes
1answer
1k 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
1answer
167 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
1answer
147 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 ...
5
votes
2answers
818 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 ...
0
votes
1answer
418 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
2answers
560 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 ...
6
votes
1answer
1k 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
1answer
698 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
3answers
611 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
1answer
1k 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
1answer
361 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
1answer
120 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 ...
3
votes
0answers
281 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 ...
16
votes
2answers
4k 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 ...
2
votes
0answers
252 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 ...
1
vote
2answers
1k 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 ...
1
vote
1answer
98 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 ...
2
votes
1answer
1k 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 ...
0
votes
1answer
251 views

Rest Test class failing in production

I have the following Rest class and test class. The test class is working good in sandbox and failed in production. I have gone through numerous post and also followed to test with Mockclass as well. ...
1
vote
3answers
3k 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 ...
9
votes
2answers
2k 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 ...
2
votes
1answer
83 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 ...
0
votes
1answer
860 views

ApexMocks: setMock() not working

I'm trying to unit test a very simple Account service but for some reason it appears the setMock() is not working. I'm using the following unit test example as a guide and I'm pretty positive I ...
3
votes
1answer
178 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 ...
1
vote
1answer
2k 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 ...
0
votes
1answer
248 views

How to write test class for http callouts?

Hi I am writing test class for callouts class any one help on this. This is my code: public class recoverrecords{ public static string response; public static list<string> first = new List<...
9
votes
1answer
365 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 ...
1
vote
1answer
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<...