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 ...
14
votes
Accepted
Test.setCreatedDate fails on note record created in test context
I've raised support case 13752643 for the problem with Test.setCreatedDate not working with a Note that was created in the test method transaction. I'll relay the important updates from the case here.
...
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 ...
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 (...
12
votes
Accepted
test fails because of TestMethod do not support getContent call
There was an update made in winter 16 which states that getcontent is also considered as a webservice Callout .So you may consider writing your test class using mock callouts .Never use get content ...
11
votes
Accepted
Test class passes in MavensMate, but fails in Apex Test Execution
This sort of thing has been going on since the Summer '08 release. See How to avoid MIXED_DML_OPERATION error in Salesforce tests that create Users.
Essentially, there are certain types of sObjects ...
10
votes
Test.setCreatedDate fails on note record created in test context
For argument's sake, let's assume SFDC will fix the issue with
Test.setCreatedDate(n.id,someDateTime)
but, until then, two workarounds
JSON deserialize
Note n = (Note) Json.deserialize('{"...
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 ...
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 ...
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-...
8
votes
Accepted
Query works in Anonymous Window but fails in Test Class
Test classes by default cannot access real data. It can access only the test data created within the test class. You can read the Understand Test Data article to understand this in detail.
You can ...
8
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 ...
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 ...
7
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
6
votes
After winter 16 upgrade, started receiving error for test failures while deployment:Methods defined as TestMethod do not support Web service callouts
My org is experiencing the same issue after our upgrade to Winter 16.
The old error message of: Method defined as testMethod do not support web service callouts, test skipped is now giving an Test ...
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',
...
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:
...
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() ...
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 ...
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:
...
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 ...
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 ...
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
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'...
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.
@...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
failing-tests × 511apex × 337
unit-test × 314
code-coverage × 103
trigger × 60
deployment × 27
callout × 21
httpcalloutmock × 20
test-setup × 19
batch × 14
soql × 13
class × 12
managed-package × 11
scheduled-apex × 11
visualforce × 9
salesforcedx × 8
future × 8
process-builder × 7
after-trigger × 7
null-pointer × 7
apextestrunresult × 7
error × 6
exception × 6
schedulebatch × 6
dmlexception × 6