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
22 views

Unable to fetch accountId of User in Test class

I am stuck in scenario while writing logic for my Test class. I am not able to access Contact.AccountId of User created in test case. I have created a TestUtility class for creating the community user....
user15754268's user avatar
0 votes
1 answer
43 views

Need help increasing the code coverage for this trigger

I'm a newly hired Salesforce admin and I've been struggling to deploy this trigger since this code is completely new to me. I've been trying to deploy a new version of a trigger with, at most, 10 ...
Humberto Cárcamo's user avatar
-3 votes
0 answers
22 views

How do I write a test class for this trigger? [duplicate]

trigger PreventChecklistAgendaItemRecordsFromDeletion on EN_Agenda_Item__c (before Delete) { List<RecordType> recordtype = [Select id,Name,SobjectType from RecordType where Name = 'Checklist'...
Divyashree h's user avatar
0 votes
0 answers
33 views

Schedulable batch Test execute org data and throw database Error

Test class User u = new User(Alias = 'xxx', Email='xxxxx', LastName='Testing', LanguageLocaleKey='en_US', LocaleSidKey='en_US', ProfileId = xxxx, TimeZoneSidKey='xxxxx', ...
SFL's user avatar
  • 3
0 votes
1 answer
30 views

ContentDocumentLink Trigger successfully blocks file upload on Org but fails in Dev Console

I want to block file uploads (ContentDocumentLink) on a FundingOpportunity object when status is Canceled or Closed. It works fine in the org, but it fails when I tried it on Dev console. A File is ...
ash's user avatar
  • 1
0 votes
1 answer
33 views

force:apex:test:run throws error in CI: "No tests were able to be executed on the org. Please verify that the source code has been pushed correctly"

Upon attempting to run Apex tests via CI using command "force:apex:test:run" in a packaging org, I get the following error: "An unknown exception occurred. No tests were able to be ...
Andrei Migachev's user avatar
0 votes
1 answer
38 views

Apex - 0% Code Coverage!

I've developed a trigger that converts a currency field, Amount (i.e. $1,000) into words, Currency_text__c (i.e. One Thousand). This is achieved via Salesforce a 'FundingAwardTrigger' on the ...
Jean-Se's user avatar
  • 43
0 votes
1 answer
20 views

"Connection was cancelled here" error NOT related to parallel testing

I have a simple Custom VF Controller with a constructor and a single method, and an accompanying test class which I have inherited. Nothing special; the controller queries some records and inserts ...
Jason Clark's user avatar
  • 11.8k
1 vote
1 answer
51 views

Apex Mocks - Verify that child is correctly related to a previously registered parent in Unit of Work

I have code where in the same Unit of Work I'm inserting a record and a related record, for example an Account and Contact. Account acc = new Account(Name = 'Test acc'); unitOfWork.registerNew(acc); ...
Kamil Pilarski's user avatar
0 votes
1 answer
56 views

B2B Commerce: Error getting or creating webcart

I'm trying to run some apex tests in our Webstore (there's just one in the org), in a scenario that the test class creates dummy user/account/contact/buyer group/products/cart to finally test some ...
Diego Catharino's user avatar
-2 votes
0 answers
38 views

Unit test is providing 23% coverage please help me to debug this [duplicate]

Unit test is providing 23% coverage please help me to debug this. Batch: global class UpdateAccountCustomerTypeBatch implements Database.Batchable<SObject>, Database.Stateful, Database....
user94116's user avatar
0 votes
2 answers
57 views

Can we compare a currency field and number field in system.assertEquals ...If yes ...How?

@isTest public class HandlerClassTest { @isTest public static void InsertTestMethod() { List<Account> accList = new List<Account> (); for(Integer i=0;i<5;i++) { ...
Akash's user avatar
  • 1
0 votes
1 answer
32 views

Test class for batch with another class called in it: Batch has 100%, but called class only 24% coverage [duplicate]

Batch class: public class BatchClassForCustomer implements Database.Batchable<sObject>{ public Database.QueryLocator start(Database.BatchableContext bc){ string query='...
Pratik Bokade's user avatar
0 votes
0 answers
44 views

Test class for @future method called by trigger not working (Methods defined as TestMethod do not support Web service callouts) [duplicate]

I have a trigger on Account that makes a call to an apex class: trigger AccountAddressTrigger on Account (after insert) { Id patientAccountRecordTypeId = [SELECT Id, RecordType.DeveloperName FROM ...
happyLion's user avatar
-1 votes
1 answer
17 views

Unexpected Tokens on Test Class for Apex Trigger: What am I doing wrong to create a test class for this Trigger trying to deploy to Production?

@isTest public class LeadRemovalTestClass { Static testmethod void updateRecord() trigger UpdateLeadOnRemoval on ActionCadenceTrackerChangeEvent (after insert) { ...
Mark5000 Crimper's user avatar
2 votes
1 answer
422 views

Test class in managed package is failing with Internal Salesforce Error: 415122577-6508 (-1655455180) (-1655455180)

When Managed package metadata is deployed to a scratch org and tests are run by automated pipeline, one of the test method in a test class is failing with following error "Internal Salesforce ...
Vasudev Pai's user avatar
3 votes
2 answers
111 views

Is There a Best Practice for Associating Apex Test Classes to Apex Classes?

About a year ago, I developed an Apex project that involved a trigger/handler. It included the trigger, a trigger handler class, several helper classes, and a test class. All went well. Recently ...
Ron Van Aken's user avatar
0 votes
1 answer
81 views

Running and filtering reports in Apex

I just spun up a brand new Dev Org (00DHp000002yxab), and copy-pasted code from this Salesforce support article: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/...
Charles Howard's user avatar
0 votes
0 answers
54 views

Test Class - Testing Updates

I had to add a couple of updates to my class and now I'm only at 69% code coverage. How do you write tests when you are simply doing an update? I feel like I'm overthinking it, but then maybe not ...
Heather's user avatar
  • 51
-1 votes
1 answer
33 views

Test Class for method with List<List<Ids>>

Hi Salesforce Experts, I have a created a apex method which is invoked from a screen flow to mass approve records selected by the user from the data table component. This is working as intended, ...
Sugi's user avatar
  • 3
0 votes
1 answer
43 views

Lookup query exception while using USER_MODE

I am trying to create a 2GP package. But whenever I try to create a version by running and validating all test classes and metadata, the test class fails and shows such an error. "common.apex....
AJITHKUMAR VP's user avatar
0 votes
0 answers
16 views

test class for get task records from zoho crm using apex? [duplicate]

HttpRequest req = new HttpRequest(); req.setEndpoint('https://accounts.zoho.in/oauth/v2/token?refresh_token='+reftoken+'&client_id='+client_Id+'&client_secret='+...
user140015's user avatar
0 votes
1 answer
86 views

Schema.sObjectType.(...).isUpdateable() method is returning true but should return false

In short, I'm trying to follow the PMD Source Code Analyzer Project, and I realized that before making an update I must validate whether the user has permission to change the field (ApexCRUDViolation)....
Noops's user avatar
  • 3
0 votes
1 answer
61 views

Custom Object Field Name Values Are Not Stored or Not Available As Referenced Data in Lookup?

We have three custom/standard objects (Opportunity, Package__c, Economics__c) referencing somehow Plant__c. Upon creation of Plant__c, public static Plant__c createTerminal(String country){ Plant__c ...
Arvin's user avatar
  • 3
3 votes
0 answers
110 views

Platform Event triggers for Subscription Management events cannot be covered by unit tests

As part of the Subscription Management license, certain platform events are fired when an Asset is updated, an Invoice is processed etc. For example the CreateAssetOrderEvent. Currently we are using ...
shooting at the moon's user avatar
1 vote
1 answer
169 views

How to I mock a void service that returns values in one of its arguments?

Let's say I have a typical fflib service class with a void method (Application.cls omitted for clarity) public class MyService { public static void doStuffInSitu(String someContext, MyWrapper ...
cropredy's user avatar
  • 69.9k
1 vote
1 answer
82 views

How to use Stub Api in order to create AuthSession records in test class

My Main method : @AuraEnabled public static void endUserSession(Id userId){ List<AuthSession> uSessions = [ select Id , UsersId FROM AuthSession WHERE UsersId =:userId AND IsCurrent = ...
Fark's user avatar
  • 17
0 votes
1 answer
65 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
  • 17
1 vote
0 answers
54 views

Manually loading apex test code coverage from sfdx command into vsCode

Whenever I run tests through vsCode, the tests take very long time to complete (a single test could take up >10 min or they time out with the "Your request exceeded the time limit for ...
Luis M Valenzuela's user avatar
1 vote
1 answer
79 views

How to write to a non-writeable field such as formula fields or CreatedDate during unit tests?

TL;DR How can I write to non-writeable SObject fields while running apex unit tests? Background While creating unit tests that check a method's functionality independent of any DML functions, it is ...
Jwok's user avatar
  • 767
0 votes
1 answer
95 views

System.DmlException: ConvertLead failed. First exception on row 0; first error: RECORD_IN_USE_BY_WORKFLOW

I am trying to convert a Lead in a Test Class but i'm facing the exception above. I searched about this error but it's didn't helpfull. I try to follow these steps to search for a solution: https://...
Erick Petzold's user avatar
3 votes
1 answer
86 views

Should triggers be inactive by default during the creation of test data?

Some time ago, I asked about this topic in the post Should be Triggers inactive by default in Unit Tests? However, my question wasn't precise enough. I'd like to pose the question once again, this ...
Piotr Gajek's user avatar
  • 2,555
-1 votes
1 answer
56 views

Trigger test issue: opportunity amount becomes null in trigger also it's set in test class, other fields retain set value

I developed a trigger to calculate converted amount in a multi-currency setup, and store the value in a custom field called Converted_Currency__c on the opportunity. The trigger works perfectly in ...
Olivier Boudry's user avatar
1 vote
1 answer
52 views

Troubleshoot Test Class for Schedulable Apex with Email Message

My schedulable apex is working properly and I have 100% test coverage on it, but the test is not giving me the System Assert I am expecting and I'm hoping someone can spot the reason why. The Apex ...
Kendra Broom's user avatar
0 votes
1 answer
136 views

System.DmlException: Insert failed. First exception on row 0; first error: INVALID_CROSS_REFERENCE_KEY, Owner ID: owner cannot be blank: [OwnerId]

I wanted to write test class for Triger which insert contact on user Insertion. So I am writing test class to check whether new created contact is related to that user or not but Im receiving error. ...
Sajid's user avatar
  • 13
0 votes
0 answers
25 views

test class covering only 53% [duplicate]

Test Class: @isTest private class OpportunityTriggerHandlerverifiedIOTest { @isTest static void testOpportunityTriggerHandler() { // Create test data Opportunity ...
swaroopa's user avatar
0 votes
0 answers
60 views

Partial mocking using ApexMocks Framework

I want to perform partial mocking using the Salesforce FFLib ApexMocks framework. For example, I have a class like the one below: public class PropertiesServiceImpl implements IPropertiesService { ...
Seleukos2's user avatar
-1 votes
1 answer
32 views

Help with StandardSetController Test with redirect

I have a visualforce page that grabs the ids from a list view of the selected records. Then redirects to lightning component. I'm having trouble with my test class. I'm not able to get the assertions ...
Jeff Bieber's user avatar
-1 votes
2 answers
79 views

How to do code coverage for Trigger/create a test class and trigger handler for trigger [duplicate]

First, I am totally new to Apex Trigger, I'm still learning the Apex Class and Triggers. I just found a code that actually works in our org. So what it does are below. get the record type id and ...
Paul Grael's user avatar
1 vote
1 answer
79 views

Unable to query PricebookEntry in ApexTest Class

I am writting Apex Test Class, but facing the issue, of unable to query PricebookEntry. Actual Error: (System.QueryException: List has no rows for assignment to SObject) Here's the logic in a ...
prem22's user avatar
  • 538
-2 votes
1 answer
38 views

Apex Test Class and Trigger Help

I am trying to write a trigger that will not allow an account with an opportunity to be deleted. The test says I have 60% coverage and i'm not covering the bottom part of my trigger handler. This is ...
Artimus Fowl's user avatar
0 votes
1 answer
99 views

while creating a test class for my apex trigger, I am getting an error 'Invalid type: Schema.SingleEmailMessage' What should I do? [duplicate]

@isTest public class TestContactEmailTrigger { @isTest public static void testTrigger() { // Create test data List<Contact> contacts = new List<Contact>(); ...
Atharva Sharma's user avatar
0 votes
1 answer
173 views

Using System.Assert in test class

I am looking at fixing this test class issue where I get an error saying "Apex unit test classes should have at least one System.assert() or assertEquals() or AssertNotEquals() call" My ...
Smita Hodiggeri's user avatar
-2 votes
1 answer
54 views

Getting System.NullPointerException: Attempt to de-reference a null object in test class. I am getting error in Apex class while querying the Account [closed]

public class AP20Case{ public Static Void UpdateAccOwner(List<Account> AccList){ List<Account> Acc = [Select id, RecordTypeId from Account where Id IN :Trigger.newMap.keyset() ...
sfdcram's user avatar
0 votes
1 answer
111 views

Test class for Agent Work not working

I have an apex class where I am trying to query for the Agent work to calculate the agents available Capacity. The Apex Class is like below public class GetAgentOmniCurrentCapacity { @...
user4912134's user avatar
0 votes
2 answers
54 views

Cannot Increase Code Coverage for Salesforce supplied Lead Conversion Apex Class

I am trying to utilize this Apex Class supplied by Salesforce but when i try to deploy to production i get a code coverage of 68%: global class ConvertLeadAction { @InvocableMethod(label='Convert ...
Zack Gibson's user avatar
0 votes
1 answer
41 views

MIXED_DML_OPERATION, DML operation on setup object

My Apex Class public with sharing class WeekendTimeSheetAlerts implements Database.Batchable<sObject>{ final String weekToSubmit; final String query; final Date weekMondayDate; ...
YETURU CHENCHU's user avatar
-3 votes
1 answer
39 views

Field is not writeable: Quote__c.Name [closed]

the fields below are roll-up summary, how should I write the code to avoid the error. Quote__c testQuote = new Quote__c ( Name = '', Quote_Total_Value__c = '', ...
Abhishek Srinivasan US's user avatar
0 votes
0 answers
39 views

Need help on my first Test Apex class [duplicate]

I have written an Apex class which works well in dev environment, but I am having a tough time writing a test class that will give me more then 59% coverage. I tried using snippets from this and other ...
Sergio's user avatar
  • 1
1 vote
1 answer
40 views

How to simulate (in test) record being deleted while callout is in progress

Let's say you have this pseudo code for some asynchronous transaction that processes an accountId Account[] accounts = [SELECT Id, Name FROM Account WHERE Id = :accountId]; if (accounts.isEmpty) {...
cropredy's user avatar
  • 69.9k

1
2 3 4 5
95