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.

630 questions with no upvoted or accepted answers
Filter by
Sorted by
Tagged with
11
votes
0answers
2k views

Test cases failing with “Could not run tests on class 01pXXX because: connection was cancelled here”

When running all the test classes in my dev org I'm getting intermittent failures on some of them with: Could not run tests on class 01p400000000001 because: connection was cancelled here The ...
11
votes
1answer
592 views

Creating Idea Votes from portal user in Test code. DML Exception

I Wrote a very simple VF Page and Controller to show the Accounts that voted on certain Ideas. Here is the controller. Just aggregates the Up and Down votes on the Idea excluding internal votes. ...
8
votes
0answers
215 views

Are ContentVersion unit tests impossible using a Community user?

I have some unit tests that pass when using System.runAs with standard user but fail when using Communities user. The error is (note the mention of NetworkId): ...
8
votes
2answers
2k views

Test class showing error : Converted Account empty for a Converted Lead.: [ConvertedAccountId]

I need to write as test class to cover public without sharing class CMassConvert { public boolean hasErrors; public String errorMsg {get; set;} //constructora public CMassConvert() {}...
7
votes
0answers
856 views

How do you use a mock response when calling Auth.JWTBearerTokenExchange.getAccessToken()?

Let's say I have a class like this /** * Example of using JWTBearerTokenExchange to demonstrate issues with testing and mocks */ public class JwtBearerTokenExample { public static String ...
7
votes
0answers
252 views

DuplicateRecordSet records not created in unittests?

Ok, so I have some code that will segment the duplicate record sets generated by the system. To give an example, in case all contacts of a duplicate record set are of a certain type, it goes to team X ...
7
votes
0answers
2k views

error from test method - “apex job is already scheduled for execution”

I have a batch apex which starts itself after x mins. this is done in finish() method using , system.scheduleBatch() method. below is how the batch looks like, global class my_ProcessPurge_Batch ...
6
votes
0answers
187 views

How to get a PageReference from Site.login(username, password, url) in an Apex Test

When I run this code from within a LWC controller: ApexPages.PageReference pageRef = Site.login(username, password, url); It returns a valid PageReference instance. But when I test the code in a ...
6
votes
1answer
1k views

How test Process Builder's scheduled actions with APEX testmethods?

We have an org where we are using Process Builder to create cases 1 hour after a record have been created with missing data. We are using a scheduled action (Call apex) to do so. We are using the "...
5
votes
0answers
81 views

Test fails with runAs, queueable and callout in specific org

Has anyone got an idea what could cause the following test to fail in one org and pass in another? The error is System.CalloutException: You have uncommitted work pending. Please commit or rollback ...
5
votes
1answer
557 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 ...
5
votes
0answers
178 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);
5
votes
0answers
399 views

System.UnexpectedException after catching an exception in unit test

I've encountered an odd situation where the presence or absence of a try/catch block results in an unusual system error: System.UnexpectedException: Error processing messages The test case is ...
5
votes
2answers
118 views

Salesforce Test accesses some org records

I'm bit new to SF. I have problem with one of the tests. Basically I create an empty test and check if records are empty. But whatever I do, ContentNote records are not 0. When debugging, I saw that ...
5
votes
0answers
401 views

Difference between deploying user and System.runAs()

I have a weird situation where some test classes will deploy to Production only if the user deploying it has a specific keyword on their LastName - "TestMode, TestsEnabled". Updating my test classes ...
5
votes
0answers
188 views

Lightning ODATA connect How to create a Test coverage case

I have tried to create a test coverage method against a database Query which goes after an external object. The external datasource is a Lightning ODATA 2.0 connector. I setup the MockHTTPcallout but, ...
5
votes
1answer
2k views

How to update parent Case when updating EmailMessage

Long time reader first question :) I have a before insert trigger on EmailMessage, but I also need to update the Parent Case - which works fine. I believe I have to use before insert as I am updating ...
5
votes
0answers
163 views

How to unit test ChatterActivity?

I need to perform some testing using ChatterActivity records. I try creating a user and inserting a chatter post as that user, but when I then query I don't get any ChatterActivity for that user. What ...
5
votes
4answers
1k views

Lead conversion unit tests throw errors for validation rules that use custom settings

I'm running into an odd issue with some of my unit tests & can't seem to find out what exactly is causing it the problem. Several of my unit tests revolve around converting a lead to an account (...
4
votes
0answers
110 views

Can we work around Owner related fields not being retrievable via SOQL in apex tests?

Background: Recently I wrote a very simple Batch to run on the 1st of every month. Its purpose is to query for any Project__c records that aren't Complete, and create a related "Carry-Over" record in ...
4
votes
0answers
88 views

Perform a deployment and execute all tests with a flag to opt out of collecting code coverage

I'd like to perform a deployment and execute all tests with a flag to opt out of collecting code coverage. The goal is to speed up Apex test execution. I'm aware that we can do it manually by ...
4
votes
0answers
273 views

Intermittent Process Builder Flow Error - How to troubleshoot with no info?

Background I'm getting a dreaded flow error for one of our test methods that occurs fairly regularly, but not consistently. I've reviewed the debug logs thoroughly, but can't see any obvious way to ...
4
votes
0answers
262 views

Run Test button/link gives error: command 'sfdx.force.apex.test.class.run.delegate' not found

In Visual Studio Code setup with SFDX links appear above each test method/class that are clickable and presumably would run the appropriate test. However these have never worked for me, I just get ...
4
votes
1answer
485 views

force:source:pull fails with Can't retrieve non-customizable CustomObject named: ApexTestResult

I deployed source to a scratch org, then ran some unit tests locally in the org. Now when I do sfdx force:source:pull I get: ERROR running force:source:pull: Can't retrieve non-customizable ...
4
votes
1answer
183 views

Testmethod verification that @future published Platform Event

Given an @future method public class MyClass { @future public static void doPublishAsFuture(){ EventBus.publish(new My_Platform_Event__e()); } } and a testmethod @isTest private static ...
4
votes
0answers
1k views

ContentPublication Limit exceeded in Unit Test

I have Apex triggers on ContentDocument and ContentDocumentLink Sobject. I wrote my Apex Unit Tests for those triggers and I was creating more then one Content Data record to test the Logic in Bulk. ...
4
votes
1answer
464 views

custom setting error when test classes run in parallel

I have a few test classes that fail but only when all the test classes are run in parallel.I'm using a hierarchy custom setting as part of the test code. In order to narrow down the problem I have the ...
4
votes
0answers
889 views

Test method related to Content fails during Deployment, but not when run all test is run

I am facing issues in deploying a change set. This contains one page and class. None of the components are new. There is a slight change in these two components. When I deploy, an unrelated class ...
4
votes
0answers
228 views

How to test controller extension for 'New' record use case?

I'm trying to create a test class for a null record. I have a vf page, and extension, that acts as a form for creating a new record. In my extension constructor I have the following code public ...
4
votes
0answers
391 views

Is it still impossible to start a Flow Interview for a VisualForce Controller's Unit Tests?

Currently looking at An Advanced Example of Using Been writing some unit tests for the VisualForce controllers I've been creating, which, with my original code would result in "Interview not ...
3
votes
0answers
339 views

How to write an Apex test for a Process Builder's scheduled actions?

I'm creating test coverage code for an autolaunched flow to push to production. Using the Tooling API in developer console, I can see that I almost have 50% coverage 4/11 elements covered (as seen ...
3
votes
1answer
668 views

Why can't I Insert ContentDocumentLink linking to Community user?

I have written a trigger class on content document link to create a task whenever a community user creates a task on a case. Currently, the trigger works perfectly when attaching a case through the UI,...
3
votes
0answers
34 views

Test SalesforceDX CLI Plug-in documentation

Does anybody knows where can I found the documentation explaining how to test SalesforceDX Plug-ins? I am asking specifically about testing SFDX CLI Plugins and not how to create the Plug-ins.
3
votes
0answers
110 views

Salesforce Connect Custom Adapter test class

What is the correct way to approach testing Salesforce Connect custom adapters? Perhaps there is a unified approach to testing all of its parts, but specifically what is the approach for testing the ...
3
votes
1answer
200 views

How to write unit test for LogoutEventStream trigger

So we use after insert trigger on "LogoutEventStream" for some actions on logout. My question is about, how to write tests for this? How do we call the insert record on LogoutEventStream? Here's ...
3
votes
0answers
1k views

Test class for community to get network id

i am facing problem with getting network id in test class, while executing visual force page, controller returns proper network id, when try to write a test class for this 'ClassNetwork' controller, ...
3
votes
0answers
255 views

Where to find a documented reference for Test.testSandboxPostCopyScript

I'm looking at the SandboxPostCopy interface for creating a sandbox post copy script. I found the Salesforce documentation covering the interface and even an example (albeit, syntactically incorrect) ...
3
votes
0answers
434 views

Test passes when run alone; fails when run with other methods in the same class

I've got a test that passes when run by itself, but fails when run alongside another method in the same class. No changes were made between test runs, and I have made sure to select Disable Parallel ...
3
votes
1answer
461 views

Case Owner changing upon case insert (all case triggers are inactive)

I'm trying to run a unit test to see if the case owner upon insertion matches an expected value. In this case, the expected case owner will be the account owner. However, upon inserting the case, the ...
3
votes
0answers
119 views

How to set up a test user in a unit test that invokes a callout from a future method

I want to do the following in a unit test: Create a test user to invoke System.runAs with Update a case record and then invoke a future method that has a callout in it Check the result Turns out I ...
3
votes
0answers
158 views

Attempting unit test, “You popped a context which you never pushed…”

Lately when trying to execute a unit test using the Force cli tool I'm getting an error I've never seen before. ERROR: You popped a context which you never pushed, check for unbalanced push and ...
3
votes
0answers
759 views

Reference field returning NULL in Test class only

We are facing a very strange issue when writing proper unit test using asserts. We have two objects which have logical link with a field. So, we wrote a batch class to relate those two objects using ...
3
votes
1answer
315 views

Test class for Rest

Rest class : @RestResource(urlMapping='/uhb/sdefs/') global with sharing class GSSRestResourceChatter{ @HttpPatch global static String doPost(){ List<ChatterDTO> fieldParsers =...
3
votes
0answers
583 views

Apex test to imitate closing a case + inserting a case comment

When using the standard Close Case button, you can provide an optional case comment. When a user provides a comment, the order of execution is somewhat peculiar. This caused us some troubles and I ...
3
votes
0answers
144 views

Developer Console Test Logs: Unable to Access Page

As I'm running the debugger I am attempting to review the execution logs from past test-executions that have completed, however, when I click the class in the Test tab of the Developer Console I get a ...
3
votes
0answers
3k views

How do I create apex tests with code that uses a custom label?

I have code that uses the value of a custom label. I'd like to create a test for it, and I'd like to test for different values of the label. However I see no way to change or create labels in a test (...
3
votes
1answer
655 views

Testing @future method Failed Assertion

I have some code that after inserting a task, calls an @future method to update some fields on the contact linked to that task (WhoId). This seems to work fine in sandbox, but I can't seem to get it ...
3
votes
1answer
481 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 ...
3
votes
1answer
157 views

Two almost identical test class, one passed one failed

Someone else created a class and test class that works fine: CRequestForm_SearchController and CRequestForm_SearchController_test. I made some edits to the class in sandbox, and copied the test ...
3
votes
1answer
927 views

Unit Test for Report

I need to write unit tests to call a method that validates the content of a Reports.ReportResultsObject. Is there a way to create reports programmatically, execute them and use the Reports....

1
2 3 4 5
13