Questions tagged [unit-test]

A unit test is code that is written and executed to test a specific portion/unit of source code. Unit tests are class methods with the testMethod modifier or @isTest annotation and are usually placed in separate test classes annotated with the @isTest annotation. Code that is executed by unit tests is considered covered. At least 75% of an organization's codebase must be covered by unit tests and every trigger must have at least 1% unit test coverage.

Filter by
Sorted by
Tagged with
0 votes
0 answers
16 views

How do i create test record for Product Media Object?

I've written a function that accepts a product id and returns the product Media record associated with that Id. I need to test that method, but I can't create a Product Media record in the test setup ...
0 votes
0 answers
10 views

Salesforce Billing Test Class: Can't Create Order Items with a Subscription Type for testing

I am working on a test class for an org that uses Salesforce CPQ, and I need to test a class that checks the SBQQ__SubscriptionType__c field on OrderItems for Evergreen subscriptions. The problem is ...
0 votes
1 answer
25 views

Can I mock out lookup relationship using FFLIB ApexMocks

I'm trying to mock out something similar to the following structure: Task.LastModifiedBy.Username; Task.LookupRelationshipToUser__r.Username; Using the makeRelationships method for this doesn't make ...
0 votes
1 answer
24 views

HttpCalloutMock is not working. Methods defined as TestMethod do not support Web service callouts

My Mock class is not being called in the test class. So it is throwing 'Methods defined as TestMethod do not support Web service callouts' error. PremCalloutServiceTest - This is the test class @...
0 votes
1 answer
27 views

MileStoneUtils apex class requires code coverage. Stops at 57%

Working off of the code provided here https://help.salesforce.com/s/articleView?id=sf.entitlements_milestones_trigger.htm&type=5 The test class does not cover required coverage specially the if ...
0 votes
0 answers
35 views

Help with Test Class for Before Insert Trigger [duplicate]

I created a trigger for a custom object (Event Registration), before the record saves the trigger will search Salesforce for the contact in a certain record type, if it exists attach the contact. If ...
  • 1
0 votes
0 answers
31 views

for loop in a test class [duplicate]

I've been struggling with covering a for loop in the below test class on line 40. I have attached both test class and original class Original Class public class CapEventsToPropertyDistanceCalculator { ...
0 votes
2 answers
50 views

Testing the Selector Layer

I have a best practice question. When using a selector layer pattern, what does the community think is the best practice around testing? I kind of hate writing a test for no reason other than to test ...
0 votes
1 answer
34 views

How to write testclass for custom exception

How do you write test class if the exception has been thrown public class UserService{ public class UserServiceException extends Exception{} public static Contact createUser(...
0 votes
2 answers
42 views

Test class coverage required for Custom metadata that turns a Trigger on and Off

can some one tell me how to cover custom metadata in test class? in particular if (Trigger_Disabler__mdt.getInstance('Account_Disabler')?.Disable_Trigger__c == true) line in the code below. Context ...
1 vote
1 answer
39 views

Apex Test Class Error: There is more than one Individual record type for Account or Contact. The record type must be unique

The exact error message is System.DmlException: Insert failed. First exception on row 0; first error: >CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, HealthCloudGA.AccountTrigger: execution of >...
  • 111
-1 votes
1 answer
28 views

Create APex object for testing

Is there a way, customer can create APEX object for testing? We are exposing APEX object for external developers to work on, but we do not expose constructor.( The object is built in the Java Layer ...
0 votes
0 answers
23 views

Setting Sales Agreement Status to Activated in Test class

I'm using Manufacturing Cloud and I need to write a test class using Sales Agreement in Activated Status. Unfortunately, Salesforce returns DML exceptions if I try to make an update to Status field. ...
1 vote
1 answer
52 views

How to Write test class for OutOfOffice Standard Object trigger in SF?

So i have a trigger as below, what it does is after inserting or updating the OutOfOffice record it will create a Out_of_Office__c custom Object record OutOfOfficeTrigger.apxt trigger ...
0 votes
0 answers
13 views

My apex class not covered [duplicate]

I need your help to know what´s happend witc my test please. I have my apex class called "CCS_InterfazCargueFactura_cls" and my test "CCS_InterfazCargueFactura_tst" but I don´t ...
0 votes
2 answers
62 views

How to cover try and catch block in test apex class

How to write test conditions for try and catch blocks in apex. My code coverage is not 100% by below code. Please help. Apex class: public static List<String> getUserName(List<String> ...
-4 votes
0 answers
45 views

Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, EmailMessage: execution of AfterInsert [closed]

System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, EmailMessage: execution of AfterInsert caused by: System.NullPointerException: Attempt ...
0 votes
1 answer
47 views

I need some help to deploy apex class that is for a visualforce page from Sandbox to Production

this is my first time trying to create an apex class and deploy it. It is very basic and is just the back end for a visualforce page. However to deploy to production I need to make a test apex class ...
-1 votes
0 answers
32 views

Default value on field is not set causing an existing test to fail

I am facing a very interesting problem and would appreciate some help solving it. I am working on an existing project, and was given a task. I created a hotfix branch from master and made my changes ...
  • 1
0 votes
2 answers
69 views

Apex test class method not working

I have the following method in my apex class: @testVisible private static Custon_Object_Case__c getExistingRecord(Messaging.InboundEmail email){ List<String> referenceId = email.references; ...
  • 15
0 votes
0 answers
37 views

How do I test child accounts? Test code shows nothing for child accounts ANSWERED

How do I test child Accounts? The test I've written finds the parent account, but returns nothing from the child query. Work: Display a button if the Account has a set of fields properly populated, OR ...
  • 63
0 votes
0 answers
53 views

How to tell where an autolaunched flow is called from?

I'm in the process of transitioning all flows/workflows/processes into Apex code for easier debugging and error fixing, and am trying to figure out where an autolaunched flow is called from. There ...
  • 21
0 votes
2 answers
44 views

Getting error for Test class for Batch apex callout (Batch chaining)

I am writing a test class for Batch A And B Im calling Batch B from A in finish method. I am not able to invoke B Batchclass from A. please suggest a appropriate test class Batch apex A global class ...
0 votes
1 answer
42 views

Put unit tests in unpackaged metadata?

We were having discussions about putting unit test data in static resources. The concern was that the test data is not needed by the application itself, only the unit tests, so do we want to have this ...
0 votes
1 answer
21 views

How to use objectContaining() or another method to assert that key/value pairs are in a request body in Jest

I am trying to assert that currencyIsoCode: 'USD' is in an HTTP request body. I am able to assert that method: 'POST' is in the request body, so I am assuming the problem has something to do with the ...
0 votes
0 answers
35 views

Apex test is successful except during deployment

I have an Apex test that tests a trigger handler. The test works as expected when running in vs-code. It also works as expected when ran via Setup -> Apex Test Execution. It fails however when ...
  • 11
0 votes
0 answers
19 views

How to write Test class for below Code? I'm calling a Json file/Application in static Resource through Composite call HTTP and loading into Object [duplicate]

public class AfterSandboxRefreshed { public static void LoadRecordsCountry(){ Organization currentOrg = [SELECT Id FROM Organization]; if (currentOrg.Id != '...
1 vote
1 answer
56 views

Test method fails with Queueable job exception

I am trying to understand the behavior of testing Queueable classes, most specifically asserting that an exception was thrown in an execution. To provide some context: I have a Queueable Class (...
0 votes
0 answers
104 views

Updating Test Class

I am super new to Apex and am trying to update some code in production. I have it working in a sandbox but the problem is getting enough test coverage. I didn't write this so I am trying to work with ...
0 votes
1 answer
31 views

How can I create test class to use the hyperlink field?

I have a custom object with formula field using hyperlink function. However I need to create a scenario in apex test class where a formula field (using HYPERLINK formula) is click. Example of a ...
0 votes
0 answers
38 views

How to Create a Test class with a Future Callout that is not dependent on an HTTP Response?

I am new to Apex, but I have been tasked with sending a Salesforce Custom Object to another Company system. My Class works as expected, but my test class fails due to having a @future method. I did ...
-1 votes
1 answer
24 views

Could we make Mandatory Field null in test class?

I have custom object ABC__c . i have mandatory custom field Account__c on this object which is a master detail. Can we make this fields as null in a test class?
1 vote
1 answer
72 views

Resolve System.DmlException: Insert failed. INSUFFICIENT_ACCESS_ON_CROSS_REFERENCE_ENTITY, insufficient access rights on cross-reference id: []

Getting these stack messages Class.TestDataFactory.InsertTestData: line 166, column 1 Class.OrderTests.SetupTestData: line 7, column 1 As I am logged in as Salesforce Admin in my sandbox, I should ...
0 votes
1 answer
17 views

Stress Testing a Scenario that is on a platform event based update

I have a specific framework that is actually updating tons of data using platform event. Now I need to do a stress test to determine what is the amount of data that the framework would actually break. ...
  • 37
0 votes
0 answers
184 views

Jest Error - TypeError: this._environment.runScript is not a function

Test suite failed to run TypeError: this._environment.runScript is not a function at Runtime._execModule (node_modules/@salesforce/lwc-jest/node_modules/jest-runtime/build/index.js:856:41) Hey, I ...
  • 1
0 votes
0 answers
30 views

Retrieved article not visible in test class

I am trying to publish an article in test class and retrieve it to test some assigned variable via triggers. Here is the code in my test class that will create the knowledge articles and then publish ...
  • 2,483
1 vote
0 answers
26 views

Skip running tests from external Unlocked package

We use Nebula Logger as an unlocked package. During a CI job we create a scratch org and run all local tests: sfdx force:apex:test:run --codecoverage --resultformat human --wait 60 --testlevel ...
  • 4,917
0 votes
1 answer
37 views

how to set user.profile.name for test data

I have to create a test class that checks for this "if" condition, and I don't know how to set user.Profile.name for test data. //System Admin User Id string userId = ...
  • 9
0 votes
1 answer
56 views

How to runalltest in package.xml and deploy to org?

I have package.xml file which contains apex classes, test classess , triggers. So, I need to runalltest in the org only run test class which is contained in my manifest file and deploy to org is ...
  • 101
0 votes
0 answers
30 views

Test class for automation with Quip documents

I have a Trigger automation that creates a mirror record when a quip document is created in salesforce. It was using hardcoded URLs and we are migrating to a different domain, so we had to change it, ...
0 votes
0 answers
94 views

System Assert Initial List Size

I have a class that has a constructor that is defining a list. I want to assert that the initial size of the list is set properly. An error is being throw when I run my test class around verifying the ...
  • 3,329
0 votes
0 answers
30 views

Metadata API Deployment Unit Test Results, CodeCoverageResult.dmlInfo is always null

Trying to build a report of Deployment metrics with Metadata API. The documentation says that the CodeCoverageResult object should contain performance metrics for DML, SOQL, and methods which is ...
0 votes
1 answer
43 views

how to create test data for the condition "Opportunity.LastActivityDate != null"

In my batch class, there is a condition that checks if(Opp.lastactivitydate != null || (Opp.LastModifiedDate > Opp.CreatedDate && Date.ValueOf(Opp.LastModifiedDate) <= Opp....
  • 9
-1 votes
1 answer
57 views

Assign null value to Formula Field in a test class

Hello i have custom formula field name-sau__c on custom object NEw__c. In test class i need to passs null value to this formula field . to increase coverage of my code. formula field -Sau__c is ...
0 votes
0 answers
28 views

my test class is passed how to improve code coverage my class for --- if conditions [duplicate]

apex class-- global class AvailableTrainers implements Messaging.InboundEmailHandler { global Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email, Messaging....
0 votes
1 answer
45 views

Apex Controller - How To Access Read-Only Variable In Test Class

I've been searching for hours to write a test class for the Controller. Scenario: All Visualforce Page will use this controller to export a CSV/Excel template from List View. This Controller has ...
  • 33
0 votes
1 answer
40 views

System.QueryException: List has no rows for assignment to SObject -- in class line 12 and test class line 17

Apex-- global class AvailableTrainers implements Messaging.InboundEmailHandler { global Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email, Messaging....
0 votes
0 answers
36 views

Apex Unit Test Error

This is my code to get the access token from a wrapper class which calls a service String token = AuthorizationWrapper.getAPIToken(); Map<String, String> headers = new Map<String, ...
  • 121
0 votes
2 answers
44 views

Creating a test class with test data to test a custom apex controller

Hey I have created a custom apex controller for us to use in cases where we need to lookup IDs from salesforce based on our own IDs coming from our own system. Now I want to test this controller class ...
  • 1
1 vote
1 answer
47 views

How to use Test.setCreatedDate method with a list

I'm getting this error: Method does not exist or incorrect signature: void setCreatedDate(List<Object__c>, Datetime) With this (excerpt of) test class below List<Object__c> testRecords = ...
  • 13

1
2 3 4 5
94