2022 Developer Survey is open! Take survey.
20 votes
Accepted

How do governing limits work with @testSetup from Spring 15' release

As I have just rewritten our internal unit test design patterns I've tested this quiet thoroughly and believe I have a proper grasp on how @testSetup currently handles governing limits within the test ...
user avatar
  • 4,368
19 votes
Accepted

How to create test data for a class that checks for historical data?

You can use following: Test.setCreatedDate(recordId, createdDatetime); Sets CreatedDate for a test-context sObject. Datetime testingday = Datetime.now().addDays(-3); Test.setCreatedDate(...
user avatar
19 votes
Accepted

@testSetup vs DataFactory?

A major reason to use @TestSetup is the situation where you have many tests that require the same baseline of data. The @TestSetup method runs once and all its data is available to all the test ...
user avatar
  • 129k
13 votes

@testSetup vs DataFactory?

I use both. Why would you have to choose one over the other? @testSetup is a way to reduce test execution time by reusing the data created once in all the test methods of a test class. TestFactory is ...
user avatar
  • 2,001
11 votes

What are the advantage of the @testSetUp annotation

The product manager (I think) at the time this feature was introduced explains it like this: If you want information that is common to all tests, it can be inserted in the test setup method and ...
user avatar
  • 129k
11 votes

Avoiding Governor limits on test classes

Set up your test data in @testSetup. To avoid using your test governor limits, start with Test.startTest(): @isTest class MyUnitTestClass { @testSetup static void testSetup() { Test.startTest();...
user avatar
  • 430k
10 votes
Accepted

Retrieving records from @testSetup Method

The compilation problem is that the definition of u is local to one method and you are trying to reference it in a another method. You can get a local reference to that user to avoid the compilation ...
user avatar
  • 129k
10 votes
Accepted

Set CreatedById Field in Apex Test Class

User u = new User(); //put the user details you want for this user. insert u; System.runAs(u) { /* your record insert here */ insert Account; } The account will have the createdById set as the ...
user avatar
  • 3,954
9 votes

Why do Methods marked @TestSetup in Classes marked @IsTest count against test coverage requirements?

This has nothing to with @testSetup. My answer from a similar question which is valid for this one also: What could have happened is, test class was created without @isTest annotation (at this point ...
user avatar
  • 2,001
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
  • 430k
8 votes
Accepted

What are the advantage of the @testSetUp annotation

To be honest, the increase in speed is about the only tangible benefit to us (as developers). Salesforce themselves arguably sees more benefit from people using this annotation than we developers (...
user avatar
  • 46.6k
8 votes
Accepted

Testing with ApexMocks vs Stub API

ApexMocks uses the Stub API. The pro of ApexMocks is that you get a ready-to-use Stub API framework for your own tests, while the con would be that it may have a lot of features you don't need (code ...
user avatar
  • 430k
7 votes

how to create a Sobject object

While you cannot instantiate an non-typed sObject, you can create it generically. First, get the sObject type. For example, for "Account", I would do the following: Schema.SObjectType accountType = ...
user avatar
  • 1,437
7 votes
Accepted

How to use the annotated @TestSetup method in Tests?

Yes, you must re-query the data. Sometimes you might use constant strings to pull in, for instance a custom setting: static final String SETTING_NAME = 'foo'; @TestSetup static void setup() { ...
user avatar
  • 142k
7 votes
Accepted

Can I use the @TestSetup from a separate test class?

@TestSetup is only applicable in the same test class. You should take this approach to make your TestDataFactory class reusable and that setup() method can be accessed from @TestSetup method of ...
user avatar
7 votes
Accepted

What is the scope and level of isolation of @TestSetup data in a test class?

Your @testSetup method is run once, before any of your tests are run, and any changes made to those records are rolled back between tests. From the documentation: By setting up records once for the ...
user avatar
  • 8,532
7 votes
Accepted

Test.startTest() in testSetup method

This is a method you would have known about if you'd seen my previous answer on the subject. We use this internally because without using this technique, we continually run into governor limits on ...
user avatar
  • 430k
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
  • 142k
7 votes
Accepted

@testSetup best practice

The Test Setup method runs once and only once for the given test class (at least when running all tests; I'm not sure what happens if you run tests selectively, by method name). When a test class is ...
user avatar
  • 26.7k
6 votes
Accepted

Why does an autonumber sequence get "stuck" when creating records after a @testSetup method?

My hunch is that when you enable Independent Auto-Number Sequence Test Option, the auto number sequence gets reset for every transaction. Since @testSetup runs in a different context, it should be a ...
user avatar
  • 142k
6 votes
Accepted

Salesforce Calculates Test Coverage incorrectly if classes call each other - how to work around?

That's not how unit tests work. The lines tested in each test will be cumulatively covered. You can view the results for an individual test (assuming you turn off "Store Only Aggregate Test Data"), ...
user avatar
  • 430k
6 votes
Accepted

Create Quote related to Account in Test setup method

You can't set the accountId of Quote directly. It will be set by Salesforce with the accountId of the Opportunity related to this Quote. Ran this code and print the quote variable and it have the ...
user avatar
6 votes
Accepted

Use ANT to execute existing ApexTests

You can use the new compileAndTest task to run tests without a package.xml. Your build.xml can look like this: <project name="salesforce" xmlns:sf="antlib:com.salesforce"> <property ...
user avatar
  • 430k
6 votes

What are the advantage of the @testSetUp annotation

From my humble point of view, the biggest advantage is to not have to duplicate code and have a consistent set of data that you can use for all the tests in a given class (or classes). However, there ...
user avatar
6 votes

How to use the annotated @TestSetup method in Tests?

It is a correct way. I would, however, like to identify few benefits of @testSetup below Use this annotation if you want to create test data once and use it in all test methods of your class. ...
user avatar
  • 5,517
6 votes
Accepted

How efficient is to use Future call in Test class?

I would say no. This sounds like an attempt at "clever" programming (which I'll get back to). If you're using an @future method (or any async method) to do test setup, you'd need to wrap the call to ...
user avatar
  • 46.6k
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
  • 129k
6 votes

How to Test Omni-Channel User Service Presence in Apex Test Class

I recommend you use query mocking here. It's a flexible concept and you can build it many different ways. It's a very useful pattern for data types you cannot insert in a test context. The simplest ...
user avatar
  • 142k
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.1k
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
  • 430k

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