Stack Exchange Network

Stack Exchange network consists of 174 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.

1
vote
1answer
64 views

How to unit test with OpportunityLineItemSchedule objects?

I wrote a OpportunityLineItem trigger that works with the OpportunityLineItemSchedule items related to it. Now I want to write unit tests for this trigger, and want to use sfdx to push the code to a ...
0
votes
1answer
35 views

Cross object values are accessed as Null values in Test class

I have 3 objects ObjA, ObjB, ObjC - these three objects have relation like ObjA > ObjB > ObjC ObjB have a lookup field for ObjA ObjC have a lookup field of ObjB Created on validation rule in object ...
-1
votes
1answer
24 views

Test Class failing due to Mixed DML Operation Error on User

Error :- System.DmlException: Insert failed. First exception on row 0; first error: MIXED_DML_OPERATION, DML operation on setup object is not permitted after you have updated a non-setup object (...
1
vote
1answer
178 views

Authoring a Negative Test Class for a Calculator Class

I seem to be stuck on this trailhead section on writing negative test classes. I have the class authored as follows but as this doesn't seem to reference an sObject, I am wondering what steps to take ...
1
vote
0answers
28 views

Is there a Salesforce formula playground website / service / tool

I'm interested in using a Salesforce formula playground similar to: JS Bin - JavaScript Debugger JS Fiddle I've had a Google and cannot find anything. Does anyone know of a tool/website/service ...
0
votes
1answer
113 views

Execution AfterInsert caused by: System.FinalException: Record is read-only:

I am creating record in energy object and have to update a field with user entered value + static value while creating and updating . trigger testews on Energy__c (after insert,after update) { ...
1
vote
1answer
26 views

How to test or replicate : System.QueryException: Non-selective query against large object type

In our org, there are 220k existing user record, Based on this previous code : public static void assignOwnerId(List<Inventory__c> invList){ List<User> userList = new List<User&...
1
vote
0answers
43 views

Salesforce Contracts - Testing on Contract term/End Date

I have a class that is looking for contracts 30 days before the end Date. My problem is i am trying to build a test for this class, and in that i'm trying to create a contract where the End Date is ...
0
votes
1answer
22 views

Apex Batch Test Class Does not seem to be executing the Execute Statement

Based on the posts I read it seems like the consensus is the test record is not being created but I believe it has enough to create. The execute part of the class never seems to fire. The actual ...
2
votes
2answers
78 views

Errors after Namespace Addition for Managed Package

I am about to create a new managed package on top of Financial Service Cloud and I noticed I get errors on my test classes after adding the namespace. I tried copying a line and run it on dev console. ...
1
vote
1answer
35 views

Insufficient Code Coverage for Batch Class

global class EmailNotificationBatch implements Database.batchable<sObject>,Schedulable,Database.Stateful { /* Getting data from the account */ global Database.QueryLocator start(...
-3
votes
1answer
50 views

how to write test class for below code [closed]

Apex Class public void run() { String currentuserid=UserInfo.getUserId(); List<Id> agentIds = new List<Id>(); for(User agent : [SELECT Id,ManagerId FROM user ...
0
votes
1answer
39 views

Test Coverage for Pattern Matcher in Apex

I have the below code in my controller Class in a Verify () function - public class SA_reCAPTCHA { private Reference__c Reference; public List<Reference__c> RefID = new List <...
-3
votes
1answer
24 views

Test class example for Push Topic [closed]

I am unable to get 95-100% test coverage for below class here is my class global class BT_Invoicing_New_Override_Crtl { public ApexPages.StandardController stdCon; public BT_Invoicing__c ...
0
votes
1answer
20 views

How to test catch-part of the method? [duplicate]

I tried to put null rows and do many another things. I can't test this catch. It writes System.QueryException: List has no rows for assignment to SObject. public List<MyObject__c> getList(){ ...
0
votes
1answer
26 views

How to fix wrong test controller's filter by status?

public void byStatus(){ //Temporary multipicklist, because my main picklist isn't multiselected: String temporary = jobAdvertisement.Temporary__c; List<String> temporaryList = new ...
0
votes
1answer
28 views

Verify that code was not executed

In a unit test class how can we verify that code didn't run or a DML statement wasn't executed because a condition wasn't met? Contrived Example (Updated): public static void DoStuff(List<Contact&...
1
vote
0answers
40 views

Using Named Credential URL in Apex Class returning null static resource in test class

I am implementing Named Credentials to replace how we do callout for a certain endpoint. One of my current test methods looks something like this: @isTest private static void test_method1() { ...
0
votes
1answer
87 views

Test class for method with API call and return type String

How do we write test method for Apex class used by Lightning component where my return type of method is String but within the method I call rest api Here is my class method: public static String ...
6
votes
2answers
47 views

Does SFDX add any way of automating the setup of business hours?

It wasn't (and I assume still isn't?) possible to programmatically set up business hours via Apex code in unit tests: Set Business Hours in Apex Test Code Creating Holidays and Business Hours in Test ...
1
vote
1answer
49 views

Apex tests cannot be enqueued during a test ERROR

Below is the excpetion, System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, Apex tests cannot be enqueued during a test. It happens ...
1
vote
1answer
26 views

How to increase code coverage for catch block

I have below Apex class (DAO class) which has a try-catch block. Same try-catch block has been used in 5 different methods. I tried sending setOpportunityId as account id or null id but still, it is ...
-2
votes
1answer
32 views

How to create ApexClass record in test class?

Here are many objects on which DML are not allowed in test class. But if our code is querying ApexClass sObject then how can we create some ApexClass records in test class to cover our code?
-4
votes
3answers
51 views

test class failure

public void gettemplatebody() { if(selectedtemplateName!='Select Template') { Emailtemplate selectedtet=[SELECT id,Name,subject,body,htmlvalue,markup,templatetype FROM ...
1
vote
1answer
194 views

Clearing or resetting a singleton in test class

I'm writing a test method to reset the value of a singleton variable, however I have no clue how to do it. please can someone assist me with the test code to test the following: public class Service ...
0
votes
1answer
26 views

Test class for Vf page controller failing

I've two vf pages QRCase (output) and QRCaseEdit(input/Edit) which are used to create cases for a specific record type 'Quality Report Case'. I tried writing the test class for controller, but it's ...
4
votes
0answers
84 views

Test Failed 'Validation Errors While Saving Record(s)' when loading Accounts with Test.LoadData

Background When I use the Test.LoadData method to load Account records: List<Account> accountList = (List<Account>) Test.loadData(Account.sObjectType, 'account_test_data'); I get this ...
1
vote
1answer
59 views

Error System.SObjectException: Business Account may not use Person Account field PersonHomePhone when loading Person Accounts using Test.LoadData

I get this error message: System.SObjectException: Business Account may not use Person Account field PersonHomePhone When I try and load Person Accounts using Test.LoadData() below is the data: ...
-3
votes
1answer
44 views

Could you please help me write a test class for the following code: [closed]

trigger TicketTrigger on Ticket__c (before insert) { List<Id> cusIdList = new List<ID>(); List<Ticket__c> updatedTckt = new List<Ticket__c>(); for(Ticket__c ticket ...
2
votes
0answers
31 views

how to write test class from Apex ConnectApi.Wave.executeQuery?

How to write the Apex test class for ConnectApi.Wave.executeQuery? ConnectApi.LiteralJson result = ConnectApi.Wave.executeQuery(JSONString);
2
votes
1answer
69 views

What is the best way to run test classes from Apex code? SOAP Apis or Rest API?

To run all tests or specific test classes we can use both the APIs SOAP and Rest API. Below are the differences that I noticed when done research on both. All test SOAP - Possible, Without ...
2
votes
1answer
76 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 ...
-4
votes
1answer
27 views

I am unable to write test class please help me out [closed]

public void sendComplaintsCaseWaitingForSevenDaysNotification(List<Case> newList){ Id complaintsRecordType = Schema.SObjectType.Case.getRecordTypeInfosByName().get('Complaints')....
1
vote
1answer
31 views

Using a formula to fetch a record in Controller - Test class Issue

I am using a formula field to retrieve a record from a list in my controller. The retrieved record's id is then used in a wrapper class, within the same controller, to create a new record with the ...
0
votes
1answer
60 views

Is getContent not supported in unit tests, even if HttpCalloutMock is used?

I have created REST Service to return content from supplied image URL. In @post method of REST Service, I have used page.getContent() method to retrieve image content. My REST Service is working fine. ...
1
vote
1answer
21 views

SysAdmin Cannot delete EventRelation in Unit Test?

I have a unit test that creates an Event (in the future) and some EventRelations (for Contacts, with isInvitee=True), and then later deletes 1 of the EventRelations, via runAs(SysAdmin). Pretty sure ...
1
vote
1answer
87 views

Methods defined as TestMethod do not support Web service callouts - Unit Test [duplicate]

We have a custom object called 'support' - we have process builder configured for this object. in one of the process builder action it will invoke Apex (Invocable Method). This will trigger a callout ...
-1
votes
1answer
69 views

Test Class/Method for TriggerHandler - Negative scenario

For this class below, which essentially counts the number of Open cases on an Account, on update/insert/delete of a Case, can anyone help me write/provide a scenario of a test method that can hit the ...
-1
votes
1answer
38 views

Need help with a Test class coverage

Hello guys i need help to cover this Trigger on a Test Class, this Test Class also covers another class so some Data is not refering to the Trigger. This is the Test Class: @isTest private class ...
0
votes
1answer
102 views

How to use if(!Test.isRunningTest()){} for a specific test

I have a class with a callout and a unittest mock for the callout. The callout has a scheduler. If I create a unittest for the scheduler the scheduler tries to do a callout If i use if(!Test....
0
votes
2answers
41 views

How to write a unittest for scheduled apex

I have a scheduler for a callout class. I am trying to write a unittest for my scheduler but do not know how to test the execute part Class global class or_guestsSchedulable implements Schedulable{ ...
2
votes
2answers
68 views

Test a schedulable class with callout by directly invoking `execute`

I have a Schedulable class. My execute method has a future callout in it, and running it by using System.schedule doesn't run that callout: Test.startTest(); MySchedulableClass msc = new ...
0
votes
1answer
39 views

How to code coverage for aggregate for loop?

public class WCT_CountryrealignUpdate2{ @InvocableMethod public static void NewcountyUpdate2(List<id> countyIds1){ Boolean t; List<New_country__c> ...
1
vote
1answer
138 views

PageReference.getURL() returning URL:[delegate:{MY RELATIVE URL}]

Im am trying to get the URL of one of my PageReferences whenever i call its method to return it to me i get this result URL:[delegate:{MY RELATIVE URL}]. Anyone know if why it's returning this, Please ...
0
votes
1answer
21 views

How to test 'throw new MyException()' in HttpResponse Callout?

I always get an Fail when try to test the negative scenario. How can I fix it? Sorry, I'm just learning. Callout: public class MyException extends Exception {} public class Token{ public String ...
0
votes
1answer
17 views

Illegal assignment from List<or_guestsJSON> to or_guestsJSON

I an trying to write a testclass for a JSON object but het the error: Illegal assignment from List to or_guestsJSON. How do I annotate the list in my testclass? Class public class or_guestsJSON ...
0
votes
1answer
26 views

How can I test my Web Token and posted record from Post Callout?

I try to use HttpCalloutMock to test my post callout. But I become System.NullPointerException: Attempt to de-reference a null object. I need to test and web token, and posted object. I'm just ...
1
vote
1answer
96 views

deployment using change set- run specified test

Hi i have a changeset which has 2 apex class and their test classes with 65% and 80% individually test coverage, if i choose a option of run specified test, will that be deployed? Because one of apex ...
-1
votes
1answer
70 views

How to fix Attempt to de-reference a null object in test for Post Callout?

I become an error System.NullPointerException: Attempt to de-reference a null object in my test: public class Token{ public String accessToken{get;set;} } public static String accessTokenBody(){ ...
1
vote
1answer
28 views

How can I test Post Callout with web token's access? HttpCalloutMock

My POST Method (data for web token - is my custom object's fields): public class Token{ public String accessToken{get;set;} } public static String accessTokenBody(){ //our web token (data is in ...