Skip to main content
17 votes
Accepted

Calling addFields in Extension class works on page, fails in test class

Indeed you cannot call addFields from a test. This is one of the very few cases where you just have to check Test.isRunningTest. You will get total coverage, however, if you one-line it. Declare your ...
Adrian Larson's user avatar
  • 151k
13 votes
Accepted

How to create Report (not report data) in Apex Test Class without using SeeAlldata=True?

This is one of those rare situations that you can't mock the data, nor perform a DML operation. You have two basic choices: use SeeAllData=true, or use the Analytics API in your code (basically, use a ...
sfdcfox's user avatar
  • 497k
13 votes
Accepted

Salesforce 2020 release has blocked all Apex tests that use http mocking

This should now be fixed, Summer '20 patch 7 is live on almost all Summer 20 sandbox pods as of this edit This was caused by an attempt to fix a different callout testing related bug in Summer '20 (...
ca_peterson's user avatar
11 votes
Accepted

SOSL not returning record in test

SOSL doesn't return results in test classes, so you have to use special methods to set the results. To ensure that test methods always behave in a predictable way, any Salesforce Object Search ...
Aidan's user avatar
  • 13.7k
10 votes
Accepted

Spring 22 testmethod GACK when assigning a PermissionSet to a newly-created user

Fixed - This will be handled by Spring '22 Patch 4.6. Update: There is now a Known Issue - Test classes are failing with Salesforce Internal Server Error. The workaround listed is to update the ...
Kris Goncalves's user avatar
9 votes
Accepted

How do I create Test Data for an Org. Wide Email Address?

You can't set up an OrgWideEmailAddress. Attempting to do so gives this error: DML operation Insert not allowed on OrgWideEmailAddress Fortunately, you can see them perfectly fine without ...
sfdcfox's user avatar
  • 497k
9 votes
Accepted

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

I've checked this GACK ID in internal logs and it points to Java.lang.IllegalStateException: commits are not allowed because someone called setCommitAllowed(false), justification: ApexTestSetupRunner-...
Swetha Maddali's user avatar
8 votes
Accepted

What should the negative tests include?

Usually, we don't throw exception from the testmethods, rather we catch the exception and put them in proper way through assert statement. like this: public class MyClass { public String ...
Santanu Boral's user avatar
8 votes
Accepted

Queueable apex runs in System admin mode even after written in System.runAs mode

I found, all the way back in 2018, that batches do not execute in the context of the System.runAs user during test executions (read more here, and feel free to upvote that idea to fix this broken ...
Phil W's user avatar
  • 37k
7 votes
Accepted

How is it possible for @TestSetup to effect test result?

This is being caused by a "recursion blocker" (as mentioned by David Reed). The fix is to remove or correct the recursion blocker from your trigger logic. The reason why this happened is ...
sfdcfox's user avatar
  • 497k
7 votes
Accepted

sfdx force:apex:test:run all local tests failing

Unlike Production orgs, Sandboxes, Scratch Orgs, and Developer Orgs only compile some of the code when you make changes. As such, it's possible to cause a class or trigger to become invalid by ...
sfdcfox's user avatar
  • 497k
7 votes
Accepted

Unit Testing problem with Product2 records: A Product with this SKU already exists

I had the exact same problem, even flagging (SeeAllData=false) explicitly. It appears that even in test cases SF validates the SKU across all product records. I got around it in my test classes by ...
S Vai's user avatar
  • 86
7 votes
Accepted

How to mock System.isFuture()?

You can solve that exactely like you solve many other aspects that are hard or impossible to test, like Multicurrency or strange org settings. You let your production code talk to a Facade ...
Robert Sösemann's user avatar
7 votes

Flaky apex tests fail unreproducible with UNABLE_TO_LOCK_ROW when inserting Product2

Overview If you hare hitting row locks consistently, there is exactly one way to guarantee you will never hit them. Bypass the database. Injection is relatively easy on both ends by using the ...
Adrian Larson's user avatar
  • 151k
6 votes
Accepted

Else part in Test Class

I'd fix the code so it's covered easier. Aside from that, you shouldn't need to set the flag within the test itself, as you're trying to validate the behavior. Here's how I'd reduce the complexity: ...
sfdcfox's user avatar
  • 497k
6 votes
Accepted

Test class Of a Trigger

You can't set it in this way oli.product2.name = 'GenWatt Diesel 1000kW'; Instead you need to give productId here Product2 prod = new Product2(Name = 'Laptop X200', ...
Tushar Sharma's user avatar
6 votes
Accepted

Unable to test the result of batch class execution

Batch class start(), execute() and finish() do not actually execute until SFDC has reached the Test.stoptest(). Thus, your queries and asserts need to occur after the Test.stopTest(). Test.stoptest() ...
cropredy's user avatar
  • 72.1k
6 votes
Accepted

How to catch a validation error exception in an apex test class

You are using a local copy of ALi yet calling the controller save method so you are saving the controller version of ali which most likely meets the validation. To assert the message was caught you ...
Eric's user avatar
  • 54.2k
6 votes
Accepted

Is it normal to tests not be able to create queues because there's one with the same name on the organization?

Setup Objects, like Queues and Users, can be queried without SeeAllData in unit tests. There's no need to explicitly create these objects under normal circumstances. The documentation says: ...
sfdcfox's user avatar
  • 497k
6 votes
Accepted

regarding External entry point

As you haven't supplied any code this is only a guess based on a typical problem... When something happens in the database changed field values (with the exception of object Id fields when records ...
Keith C's user avatar
  • 136k
6 votes
Accepted

Why won't Apex IF statement work in Production Unit Testing after working in sandbox?

This is a Known Issue that should be fixed in Spring '19. Until then, consider using a List<String> instead (as mentioned in the workaround), or use a Set<Id> instead, which should be more ...
sfdcfox's user avatar
  • 497k
6 votes
Accepted

Test classes pass in validation before deploy, but fail when i run them from "Apex Test Execution"

During deployment, Mixed DML operations checks are skipped. Here is a knowledge article regarding this: https://help.salesforce.com/articleView?id=000231160&type=1
Pragati Jain's user avatar
  • 1,580
6 votes

Set.retainAll () on API 45 dies with UnexpectedException

This is almost certainly related to the known issue The result of Set.retainAll(Set) are inconsistent between ApexCode log level is FINEST and it is FINER(or lower) being resolved in Spring '19. It'...
Daniel Ballinger's user avatar
6 votes

How is it possible for @TestSetup to effect test result?

Side effects in your test data factory, in automation fired on Account insert, or interactions between those entities and your code under test can absolutely cause this kind of behavior change. @...
David Reed's user avatar
  • 93.2k
6 votes
Accepted

test.starttest() and test.stoptest() not working

Salesforce has a governor limits! In a single apex execution context, you can only do 150 DML operations. Being a beginner to apex I suggest you read through some of the best practices highlighted ...
Mohith Shrivastava's user avatar
6 votes

Can managed packages have hidden validation rules? Keep getting Field Custom Validation Error

Apex can also run validations which cause FIELD_CUSTOM_VALIDATION_EXCEPTION, so this erro may well be caused by code. You should contact the package author for support.
Adrian Larson's user avatar
  • 151k
5 votes

Test class pattern for pricebook when standard pricebook is deactivated

To be used in custom price books, all standard prices must be added as price book entries to the standard price book. So you will still need to define a standard price PricebookEntry sPBE = new ...
Eric's user avatar
  • 54.2k
5 votes
Accepted

Case assignment rule is always executed in the test class

I have tried this and found that I can get it to both work and to fail. Let me explain how. I took the basic principle as the OP; A single Case Assignment Rule, assigning to a queue with the ...
Simon Goodyear's user avatar
5 votes
Accepted

Checking error condition in tesclass

I usually prefer to use the following pattern when you need an all-or-none test: try { delete records; System.assert(false, "Expected DMLException"); } catch(DMLException e) { // Validate ...
sfdcfox's user avatar
  • 497k
5 votes

Unable to test the result of batch class execution

The batch job won't actually be executed until Test.stopTest() is called. Move that line up above your assertions. From Using Batch Apex - Testing Batch Apex, my emphasis. The executeBatch method ...
Daniel Ballinger's user avatar

Only top scored, non community-wiki answers of a minimum length are eligible