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.

7
votes
0answers
162 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
1k 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() {}...
5
votes
0answers
209 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
0answers
362 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 ...
5
votes
0answers
324 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
321 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 ...
5
votes
0answers
151 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
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 ...
5
votes
0answers
143 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 ...
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 ...
4
votes
0answers
48 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
132 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
170 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
0answers
486 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
0answers
602 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 "...
4
votes
0answers
609 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
215 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
383 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
21 views

Creating BigObject Record in Test Class Using Test.loadData(..)

Successfully used testLoadData for a number of test classes for "regular" Sobjects. Have seen references to Stub Api Mocking Framework for one method to create test records for Big Objects in Apex but ...
3
votes
0answers
51 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
0answers
36 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
236 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
0answers
472 views

Test class fails due to Duplicate value found error

I have an object in which i have a field OrderNumber which is marked as unique.I have some records for that object in the organization. Say the OrderNumber for those records are 1,2,3,4 etc. Now ...
3
votes
0answers
101 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
130 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
546 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
0answers
467 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
86 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
0answers
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
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
0answers
73 views

Post callout behavior assertions for unit test with static resource callout mock fail

I have a working queueable callout, and working tests using static resource callout mock I can successfuly run basic assertions on the mock response body and status. However, the callout method in ...
2
votes
0answers
253 views

How to Avoid exceeding CPU Time Limit in Test Classes?

I am currently updating test classes in our org and it seems that most of them are exceeding the CPU Time Limit. They are using the @testSetup to create a lot of test data and it seems that these DML ...
2
votes
0answers
165 views

Manually aborting a job in a test class

For the purpose of testing some code, I want to be able to simulate a batch job run and abort in the middle of execution. How can I do this from an apex test? I tried using a boolean flag to abort ...
2
votes
0answers
162 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) ...
2
votes
0answers
142 views

Cannot Insert Opportunity In Test Class When Splits Are Enabled

We have Splits enabled in our org. I would like to insert an Opportunity and to assign it to a user that have Opportunity Team Members configured in his user details, so Salesforce will insert all ...
2
votes
0answers
139 views

How to test getContent() calls in Spring '17?

Now that the Apex Mocks API and its Stub API complement are available is it possible to properly test for a getContent() call? In the past I would just branch out if unit test was running and returned ...
2
votes
0answers
381 views

How to use Jasmine unit tests and still get past Lightning CLI?

Like the question says, I'm trying to work out some best practices and frameworks stuff with my team. One of the things we want to do is unit test our lightning components. I really like Bob Buzzard'...
2
votes
0answers
212 views

OrderItem does not have ListPrice set in test, contrary to non-test

This is something I've run into and worked around, but it still annoys me because I have to write test specific things in my non-test code (and can't get to 100% coverage because of it). I can find ...
2
votes
0answers
494 views

Testing a SiteLoginController Login method

I am trying to write a unit test for a custom SiteLoginController.Login() method. The method is: public PageReference login() { readUsernameCookie(); String startUrl = System....
2
votes
0answers
236 views

Flow Controller Unit Test

I have a Visualforce page and controller that calls a flow. I have only been able to get 7% test coverage. Is it possible to get more coverage even though you can't start the flow? Visualforce Page:...
2
votes
0answers
386 views

How to write test class for auto-generated JIT Class SSO

private void handleUser(boolean create, User u, Map<String, String> attributes,String federationIdentifier, boolean isStandard) I am just creating a user in SSO JIT using this handleuser method....
2
votes
0answers
2k views

System.DmlException: Upsert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY,

While deploying the code the system throws an error as: Code Coverage Failure The following triggers have 0% code coverage. Each trigger must have at least 1% code coverage. When i run the test ...
2
votes
0answers
88 views

Trigger works like a charm, tests like the Invisible Man

This class is based on Dan Appleman's TriggerDispatcher process, and is called from a centralized (TriggerDispatcher) class based on the SObject (Case) and the trigger type (Before Update). From the ...
2
votes
0answers
943 views

How to succesfully test AgentWork trigger?

I am using AgentWork trigger in my logic and I cannot achieve code coverage goal. Simple test method: Id cId = [SELECT Id From Case Where Subject = 'Case Subject'].Id; User u = [SELECT Id From User ...
2
votes
0answers
517 views

update LastModifiedDate audit field for test class?

is there any way to update the LastModifiedDate audit field for test coverage.
2
votes
0answers
55 views

What is the salesforce equivalent of Microsoft's Northwind database

This sounds like an obvious question but I have not been able to find the answer anywhere. Does Salesforce have an equivalent of Microsoft's Northwind database? My problem is that I have gone ...
2
votes
0answers
96 views

Errors in production use while deployment/validation is running

During the validation or deployment of a change set, you cannot edit Salesforce metadata. I understand why this is necessary. But sometimes, users cannot edit some records during the validation or ...
2
votes
0answers
202 views

INSUFFICIENT_ACCESS_ON_CROSS_REFERENCE_ENTITY on Communities Change Username Page Test

I created a visualforce page for Communities users to update their username and email. In real life the page works fine, logging in as a Community user I can update my username and email. However, I ...
2
votes
0answers
82 views

Centralize Object Initialization - (Insert Event with Attachment on same code)

I have a piece of code that sets a Boolean (has_Attachment__c) field to true in an After Insert context variable is executed in an Event in which an Attachment is inserted on the same time. The code ...