Stack Exchange Network

Stack Exchange network consists of 175 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange

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.

0
votes
1answer
27 views

Queueable Apex in a Before Trigger

I am attempting to create logic that, on Contact record insert or update, will compare the Phone value of the record to another reference object and conditionally populate a checkbox as a result of ...
0
votes
0answers
8 views

How to activate the FFA Billing Contract status in test class

I have installed FFA Accounting package in my org and now i have write some code on that, so when i tried to write the Test class so in my condition i want billing contract as active, but when i set ...
0
votes
1answer
70 views

Why is my apex test class failing?

There might be a lot of other ways to achieve what I am trying to do here but for practice I am using trigger. The trigger is to update a field acc_number__c in contact if the finalize_number__c ...
1
vote
0answers
23 views

Has anyone been able to set the User.IsCustomerPortal field from a unit test class?

I know the field is not editable but I mean by associating a User record with a Contact/Person Account record in your test data, somehow it gets enabled? Do you know? The reason I need this ability ...
1
vote
1answer
36 views

Apex code coverage and static boolean recursion flags

I've written two Apex Triggers, one on the Opportunity object, and one on a custom object. These two triggers keep the two objects' checkboxes in sync; setting a value on one object's value sets the ...
8
votes
5answers
3k views

How to get a list of Test Classes in an org

I need to get all test classes (classes with isTest Annotation) in an Org.. Is there anyway to filter test classes out of all the Apex Classes
3
votes
1answer
2k views

Process Builder and Test Classes

I have recently set up and activated 4 processes using the process builder to send email reminders (alerts) when records are past the due date. I needed 4 because there are 4 emails send at different ...
2
votes
1answer
2k views

Running Schedule Apex immediately in Test class

How do we write a test class for a schedule batch to run / schedule the schedule batch immediately (without using con expression). For example: Schedule batch is as below. global class ...
0
votes
0answers
3 views

Cases created for portal users before their user is created are not shared with the portal user in unit tests

I'm making some changes to a security model to rely on standard Salesforce security. When I remove a Share Set that grants edit access to an Portal User's Cases because that's redundant, and we need ...
0
votes
2answers
32 views

How to test this return value in catch exception

I'm new to test classes, can anyone give me a help on this? Here's the class: global class CallTimerController { @AuraEnabled global static Integer getMaxCallDuration(){ Id pId = ...
1
vote
1answer
41 views

Scratch org created from Bitbucket pipeline has no Standard User profile

To build DX unlocked packages I am using Bitbucket pipelines. I use the Salesforce configuration template for this. image: atlassian/default-image:2 pipelines: default: #These commands run for all ...
0
votes
1answer
25 views

Test class for a scheduled batch is not firing the batch

I have tried the class in a sandbox and works fine (i.e. it updates all of the accounts based on the attached event records). The test class refuses to fire the batch class and I get 0% coverage. ...
0
votes
1answer
37 views

testing upsert campaign members gives error: INVALID_CROSS_REFERENCE_KEY, invalid cross reference id: []

Testing my apex controller in my dev org runs fine. However, testing the same file in another dev org gives error INVALID_CROSS_REFERENCE_KEY, invalid cross reference id: [] Here is the controller ...
0
votes
0answers
16 views

Help writing a test class for apex class I want to deploy to prod [duplicate]

Public Class controller_SoqlQuery { public List<AggregateResult> medOppsCount{get;set;} public List<AggregateResult> nonMedOppsCount{get;set;} public List<AggregateResult&...
0
votes
2answers
1k views

Creating a test class for an opportunity contact role trigger to get to 75%+

I am writing a test class for a trigger that creates Opportunity Contact Roles based on the primary record. I currently have 60% code coverage and am stuck determining what steps I would need to add ...
1
vote
1answer
318 views

How to filled custom field with user id corectly by trigger?

The trigger will fire on before update DML statement on Lead object. When the Role of an old Owner (i.e. old record owner) contains string 'LG' or 'Lead Gen' and the new Owner’s Role (Role of new ...
2
votes
1answer
320 views

Run All Test Classes ( My Namespace vs All Namespace)

My Namespace : Only local classes that aren't from managed packages appear in the list All Namespace: All the classes in the organization appear, whether or not they are from a managed package. I ...
-1
votes
3answers
356 views

Test Class - DUPLICATE VALUE?

Im new in salesforce programming.. I have a trigger called CR_Approved and test class called CR_Approved_Test.. The trigger purpose = if Change_Request_Item__c.Status__c = Approved and recordType = ...
0
votes
0answers
17 views

Please assist me in writing test code for this working piece of code. I am new to testing [duplicate]

I'm stuck at 38%, Its just cover the last part of code(callout portion), I have tried several ways but it just cover till line ' parse_id.nextValue(); ' and after that line it mark as red all the ...
0
votes
1answer
16 views

pass list<string> value in test class

In my class one list of string is there which is get;set; type. i want to pass the value in test class how to do this public class MYController(){ Public list<string> selectedDelList{get;...
1
vote
2answers
52 views

Is it a good practice to use a static variable in a Test Class and use that in the actual class instead of Test.isRunningTest()?

I've read about using a static variable from a class in the test class but I'm wondering if this is legal enough to do. @isTest public class myTestClass{ public static Boolean mySwitch = false; ...
0
votes
0answers
15 views

Unit test for a update list Apex Class [duplicate]

i need to write the Unit test for my class in apex, but i don't understand how to function a Unit test, i read many articles for unit test but i don't understand how to work, my class is this, please ...
0
votes
2answers
38 views

Fatal Error while deploying apex classes to production [closed]

I have created 3 new apex classes and test classes for the same.In sandbox, test class code coverage is more than 90% for all the classes. While trying to deploy to production, I am getting error as ...
-2
votes
1answer
43 views

I am getting stuck here, I just need test apex /snippet /mock for this. Any input will be highly appreciated [closed]

I am just new to apex testing, I am getting responce from Trigger upon that i am moving further in my class. let me know id there is a way around to get better understanding to develop test cases. ...
0
votes
1answer
23 views

unit testing - if record is created with certain record type

I have a function createWithRecordTypeIfExists(String recordType). Its logic goes like this: Check whether a record type for this object exists in the org. If yes, create a new record with this ...
5
votes
1answer
583 views

Unit Test Big Objects

I have an APEX controller that writes to a Big Object. How do I unit this? During an insert operation insert I get an error: Unsupported External Object Operations.
0
votes
2answers
19 views

Create exceptions in test classes for a process builder?

Is is possible to create exceptions in a Test Class for (example) Process Builder "XYZ NAME"? To avoid the test class running that process. Right now my work-around is to manipulate the criteria in ...
0
votes
2answers
29 views

Cannot write test Quote object for Trigger

I have a Quote trigger for which I am trying to write tests. Here is my test class. However, I am getting some wierd errors while writing this. @isTest public class TriggerQuoteTest { @...
0
votes
3answers
51 views

System.AsyncException: Maximum stack depth has been reached. while writing in test class

I've got a requirement to perform queueable apex in a trigger my class working fine, but while writing a test class for queueable apex I'm getting an exception. Could anybody can help me. Trigger ...
1
vote
2answers
1k views

QuoteLineItem test class

I need to create test class for quote and quotelineitem, but i still get error message like this "System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, ...
0
votes
2answers
333 views

Workflows are not running for Unit tests

I recently came across an Issue when running a Test class. In my Test class I Insert a custom object. And by the time the record is inserted, there is a workflow rule to fire and populate the Id value ...
0
votes
0answers
25 views

How to increase the code coverage for the following Test Class, currently it covering only 50% [duplicate]

Actual Class: global class ITSSFAutoRejectBatch implements Database.Batchable<SObject>, Database.Stateful, Schedulable { global String[] toAddresses = null; global integer batchSize = 100; ...
0
votes
2answers
39 views

Testing or writing a test class for random string generator class

How can we unit test a random string generator class of this given code. I do get 100% code coverage, but I feel like the test is not executed properly. Any solutions to this will be helpful. Random ...
3
votes
4answers
6k views

How to throw DML Exception Manually

I've a Apex class as below and i've to write test class for that. trigger SetAccountStatus on job__c (after insert) { List<Account> AccSt = new List<Account_Status__c>(); List<...
1
vote
1answer
320 views

Test class for Metadata API for updating picklist values

I've created a trigger that calls the metadata api (class created from metadata api wsdl) for updating the picklist value in a sobject. Now with this working fine i need to deploy it for which the ...
14
votes
2answers
8k views

How can I assign a permission set to a user in a test context?

I have set OWD to private and create a permission set to give access to object. We have removed all the object settings access from the concerned profile. Previously with OWD set to private, profile ...
0
votes
0answers
62 views

Test class error: System.NullPointerException: Argument cannot be null

Please help me on fixing this error in my test class @isTest private class ItemizedReceiptTest { static testMethod void testMethodPostive(){ Product2 prod = new Product2( Name ...
0
votes
1answer
24 views

DmlException: Insert failed. First exception on row 0; first error: DUPLICATE_VALUE, duplicate value found: <unknown> duplicates value on record [closed]

Account parentAccount = TestUtils.createAccountWithRecordType(Schema.SObjectType.Account.getRecordTypeInfosByName().get('Parent Account').getRecordTypeId(),false); parentAccount.City__c = '...
1
vote
1answer
40 views

System.QueryException: Implementation restriction: ContentDocumentLink requires a filter by a single… in test class?

I'm facing System.QueryException: Implementation restriction: ContentDocumentLink requires a filter by a single Id on ContentDocumentId or LinkedEntityId using the equals operator or multiple Id's ...
0
votes
1answer
28 views

How do you write a test for a class that clones? [duplicate]

I have the following code that clones an asset and its' children... public class Cloner { public Asset root_asset { get; set; } public Asset cloned_root_asset {get; set; } public ...
0
votes
1answer
83 views

Test method is failing error in test class

I am fetching the data of Account from the various Instances My Apex controller: public class Accounting{ public list<Account> lic{get;set;} public list<Account> licUpdated{...
4
votes
2answers
145 views

Send out email when Apex Queueable fails and test it

This question definitely is a duplicate of many questions here, but after reading most of them I am still somewhat clueless. Question 1: why I can't just catch exceptions in the execute and send an ...
0
votes
2answers
43 views

Running an APEX class in Production, what are @isTest?

I'm trying to learn about apex test classes, I have attached the below code. I can't execute this in production developer console, but works fine on my sandbox. I was asked to write a @isTest, is this ...
0
votes
0answers
16 views

Test Class - SOQL in Test Returns No Results using Database.Insert(RecordList,DML), but works using Database.Insert(RecordList)?

I am creating test coverage for a new Lead Process we've built for our company. To properly test this deployment, I need a list of Leads to show as being Inserted by our Pardot API connector user, and ...
2
votes
1answer
25 views

How to throw or cause an Access Level exception in a test class

anyone know how to throw an access level exception in a test class to cover the below code sample? I'm assuming the object being inserted with Database.insert needs to have sharing settings modified ...
0
votes
0answers
41 views

System.ListException: List index out of bounds: 0 in test class when we try to query ProcessInstanceWorkitem [duplicate]

I am getting List index out of bounds exception when I try to query ProcessInstanceWorkitem in test class. Below is the code snippet in the main class: ProcessInstanceWorkitem[] piw = [SELECT Id ...
2
votes
1answer
98 views

LWC - Unit Testing NavigationMixin.GenerateUrl

I've been looking to start unit testing the Lightning Web Component I've created with LWC Jest and I am running into some issues in creating my component that is making use of the NavigationMixin ...
0
votes
0answers
21 views

How to write Apex test for record creation? [duplicate]

I'm having a hard time writing up a test class for the following Apex class: public class logger { @AuraEnabled public static void insertLog(string name, string id, string location, string ...
2
votes
1answer
54 views

ERROR: Cannot create an AuthInfo instance that will overwrite existing auth data

Background I get this error message: ERROR: Cannot create an AuthInfo instance that will overwrite existing auth data. In the VSCode output panel, when I try and run a unit test: Starting Run ...
0
votes
0answers
16 views

I need help with a test class unit [duplicate]

I have the following trigger and I would need some help in creating the test class. trigger listingLocationUpdate on pba__Listing__c (before insert, before update) { //Mantiene actualizado el ...