16 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 ...
user avatar
  • 143k
15 votes
Accepted

Assign a case to a queue in Apex (Test class)

Rather than setting the Case.OwnerId to the Queue ID you need to set it to the Group ID. As OwnerId is a lookup to User and Group rather than User and Queue as displayed in the UI. You can check it ...
user avatar
  • 13.3k
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. ...
user avatar
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 (...
user avatar
  • 22.2k
12 votes
Accepted

How to set both onInstall and seeAllData to true for a test method

Don't use a comma for the attributes: @isTest(SeeAllData=true OnInstall=true) As far as I can tell, this isn't documented anywhere.
user avatar
  • 433k
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 ...
user avatar
11 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 ...
user avatar
  • 433k
10 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 ...
user avatar
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('{"...
user avatar
  • 62.3k
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 ...
user avatar
9 votes
Accepted

does a test class need to pass?

If your test class fails it will lead to a failed deployment or failed package upload, so your test class MUST pass. Also, you should not write test classes to get coverage, but to actually test your ...
user avatar
  • 4,972
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 ...
user avatar
  • 433k
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 ...
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 ...
user avatar
  • 27.3k
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 ...
user avatar
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 ...
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 ...
user avatar
  • 143k
6 votes
Accepted

Test Coverage for a Email Services Class

In your EmailServices class, you have this line: @testVisible Opportunity opp = new Opportunity(); You are apparently trying to query the value of this variable in your testmethod to see if it is ...
user avatar
  • 62.3k
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 ...
user avatar
  • 127
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', ...
user avatar
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: ...
user avatar
  • 433k
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() ...
user avatar
  • 62.3k
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 ...
user avatar
  • 53.1k
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: ...
user avatar
  • 433k
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 ...
user avatar
  • 130k
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 ...
user avatar
  • 433k
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
user avatar
  • 1,540
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'...
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. @...
user avatar
  • 86.8k
6 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 ...
user avatar
  • 433k

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