18

Managed package tests are only run in the following scenarios: On package upload (only applicable to the package author) If explicitly selected to run (changing the namespace on the run dialog for Setup > Develop > Apex Test Execution, dev console, specific API calls to run them, like setting runTests in the metadata API deploy call). On metadata API ...


14

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. See Known Issue Test.setCreatedDate() does not work for Note object Update: 17th October - The known issue is now marked as "Fixed - Winter '17" Repo code ...


13

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 (specifically, this one: https://success.salesforce.com/issues_view?id=a1p3A000000ATF1QAO) that clearly didn't work as we had intended. That fix is being rolled ...


12

Don't use a comma for the attributes: @isTest(SeeAllData=true OnInstall=true) As far as I can tell, this isn't documented anywhere.


11

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 that you can't perform DML operations on in a test case and then proceed to modify other types of sObject. See sObjects That Cannot Be Used Together in DML ...


11

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 list separately, and then call addFields on the same line as your check: public Account record { get; private set; } public MyExtension(ApexPages....


10

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('{"createdDate" : "2016-01-01T00:00:00Z",' + '"title" : "foo",' + '"body" : "fooBody",' + ...


9

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 code. Otherwise they would be called coverage classes and not test classes.


9

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 inside test class @isTest public with sharing class TestRangehoodObjectionSheet { static testMethod void RangehoodObjectionSheetTest() { String Build ...


8

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 yourself using Workbench > Info > Standard & Custom Objects > Case, Fields > OwnerId > Reference To. However by doing this you will receive a ...


8

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 REST API), which means a pretty large refactor just to avoid SeeAllData=true. I would personally recommend simply using SeeAllData=true. Although this will ...


7

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 either Create your own test records in your test class and use it for your testing OR use See All Data attribute on your test class to access the real data (...


7

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 accountId {get;set;} public void myMethod() { if(accountId == null) { throw new exception ('Account Id is blank'); } } ...


6

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 accurate. Instead, you need to do this: Messaging.InboundEmailResult result = objconfirm.handleInboundEmail(email, envelope); System.assertEquals( result.success ...


6

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 Failed message. This is happening during both deployments and with running the test class manually. We have fixed our test classes and are no longer getting ...


6

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', Family = 'Hardware'); insert prod; oli.product2Id = prod.Id; So for that you need to create product record first and then assign Id.


6

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: public class RecUtility { static Boolean flag = true; public static Boolean runOnce() { return flag && !(flag = false); } } Then, ...


6

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() causes all asynchronous tasks to execute. The executeBatch method starts an asynchronous process. When you test batch Apex, make certain that the ...


6

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 would use Hasmessages method to check if there is a message Assert value of message Pseudo code Class public Asset_Line_Item__c ali {get;set;} //Need to ...


6

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: Starting with Apex code saved using Salesforce API version 24.0 and later, test methods don’t have access by default to pre-existing data in the organization, such as ...


6

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 are created) are not automatically brought back into the Apex code. So you have to requery the data to see the new values.


6

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 SeeAllData=true. I even took the time to verify it with a simple unit test: // This class is in version 43.0, SeeAllData=false by default @isTest class q224877 { @...


6

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 reliable. The reason why it happens is usually because of different log levels depending on how you run the test/deploy. For example, a change set would ...


6

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's specifically tied to the current logging level. In my Dev Org, which is currently on Spring '19 Patch 8.7 your sample test class fails with the message: ...


5

The variable od must have it's Pricebook2Id set before inserting the record. This the same design functionality as the Opportunity and Quote objects.


5

This is a known Salesforce issue. When you enable Chatter Answers, a couple of classes are created in system without its test classes. https://success.salesforce.com/issues_view?id=a1p300000008XHfAAM The details say that its scheduled to release for Spring'15. There are workarounds given there, so you can directly use them. Also you might want to check ...


5

Salesforce expects and requires 75% test code coverage on the combination of existing tests in your production environment AND the apex and tests you're trying to deploy. See the documentation for more information. Important to know is also that no test can fail, if this is the case your deployment will be rolled back.


5

You can use Test.startTest and Test.stopTest appropriately to get rid of this issue . @isTest public class addintoPublicgroup_Test { static testMethod void AddToGroups(){ County__c cty = new County__c(); cty.Name = 'Whitman'; cty.State__c = 'WA'; cty.Planting_Area__c = '1'; insert cty; Account act = new Account(); act.Name = 'Test ...


5

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 PricebookEntry(Pricebook2Id = test.getStandardPricebookId(), Product2Id = prd1, UnitPrice = 1000); insert standardPBE; Additionally, although not related, if ...


5

Deployment changed in winter 15' introducing testLevel, make sure you are setting it to NoTestRun, They might have changed the default level as in winter 15' it was set to NoTestRun but this could now have changed. Check your deployment and validation routines. ** UPDATE Critical Update in the Winter '16 Release Who is impacted by this? Any partner whose ...


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