Questions tagged [test-setup]

The tag has no usage guidance.

Filter by
Sorted by
Tagged with
19
votes
1answer
4k views

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

The new @testSetup method is covered pretty well by Jesse Altman in his: New Spring ’15 Feature: @testSetup article but as mentioned in the comments, there is a question about how this new feature ...
12
votes
5answers
3k views

What are the advantage of the @testSetUp annotation

Before Salesforce introduced @testSetup annotation in 2015, I used to create common data in a method and call it in every test method. Ex: @isTest private class clsAccountTest { private static ...
10
votes
2answers
805 views

@testSetup vs DataFactory?

When testing, how/when will @TestSetup annotation be preferable to a "DataFactory"? I understand how both works, but I don't see any case where I'd rather use the @testSetup annotation. I use this ...
10
votes
1answer
909 views

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

I have a class that processes some data returned by a query. The query is based on the createddate column of the object. In this case, the query specifically retrieves data created 3 days before today....
10
votes
3answers
919 views

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

Every test class on my org that has a TestSetup annotated method shows 0/n lines covered in the developer console (Where n is the number of lines in the annotated method). I thought maybe this was ...
10
votes
1answer
277 views

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

I ran into this while writing unit tests for my org but I simplified it in a developer account to make sure it would happen there. I have custom object Thing__c. The Name field is autonumber. I wrote ...
10
votes
1answer
505 views

Are @future methods ignored in @TestSetup blocks?

Ok, this is going to become a quite long question. The main goal is to document strange platform behavior and maybe learn more why this happens. It all started with that test which failed because ...
9
votes
2answers
357 views

Does assert methods needed in @testSetup?

I ran force.com security scan on my managed package i got the following report Query: Test Methods With No Assert L 4: @testSetup static void defConnSetup(){ so is it necessary to add assertion ...
8
votes
3answers
11k views

Retrieving records from @testSetup Method

I'd like to create records in @testSetup for my Test Class, to avoid the need to create the same records for each of the methods in the class. I also need to create both a User record and Custom ...
8
votes
2answers
727 views

Testing with ApexMocks vs Stub API

We are looking into using ApexMocks to improve the tests in our managed package. The problem is that it looks like we will need to bring it into our package which will increase the number of classes/...
6
votes
2answers
1k views

Set CreatedById Field in Apex Test Class

I have created a trigger on the Account object that is validated only if the records are being created by an non-admin account. In order to test this functionality I would have to set the ...
6
votes
1answer
344 views

Summer 15' Unit Testing @testSetup DUPLICATE_VALUE Issues

After wasting an entire day attempting to deploy a huge release to production I'm graveling at the feet of the Salesforce community. So many issues when attempting to package my release (including ...
6
votes
3answers
853 views

@testSetup method and System.CalloutException: You have uncommitted work pending

Apologies ahead of time for the lengthy post, but I'm receiving the following error when attempting to run the test method below. The error occurs when the upsertProject method is called. System....
5
votes
2answers
11k views

How to use the annotated @TestSetup method in Tests?

I'm working on how to use methods annotated with @TestSetup. I didn't find a way to pass the caseId from the initData method to the methodTest, so I request the case with limit 1. Is that the right ...
5
votes
1answer
1k views

Create Quote related to Account in Test setup method

I'm trying to assign account Id to quote, but i can't even compile code, becouse of this error message: Field is not writeable: Quote.AccountId Sample code: Account acc = new Account(name='Test Acc')...
4
votes
1answer
1k views

Avoiding Governor limits on test classes

We have a custom application created on force.com platform which has a lot of unwanted SOQL Queries and DML Statements written in a poor format. We are trying to stabilize the code now and looking ...
4
votes
2answers
1k views

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

I need to create an Org. Wide Email address for my Test Class, and I can't seem to find documentation for this. One forum led me to believe it wasn't possible, but it wasn't clear if I was ...
4
votes
1answer
2k views

Test Data Factory Methods counting against code coverage

I have created a Test Data Factory helper class to create Test Data for testing my apex code like suggested in the documentation here: https://developer.salesforce.com/docs/atlas.en-us.200.0.apexcode....
4
votes
1answer
261 views

Do governor limits like Apex CPU time limit apply to @testSetup methods?

I have a test method written by a third party that occasionally results in Apex CPU time limit errors. The part of the method that includes setting up a semi-large amount of data on multiple objects (...
4
votes
1answer
583 views

Salesforce System Error when using Test.loadData for User.sObjectType, improvements of Test.loadData with custom code

I'm starting to use Test.loadData and am facing an issue when creating Portal User data. Code below works very well, not only creating and inserting Accounts and Contacts, but also establishing proper ...
3
votes
2answers
168 views

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

While refactoring a test to make it more consistent with the style of our other tests, a colleague came upon something really weird. The original test created the test account in @TestSetup, like so: ...
3
votes
2answers
517 views

Invoke Test Execution (Run Test) using Apex Code [closed]

I don't want to run the apex code using the traditional approaches provided by the salesforce. I want to run the apex test execution using the Apex Code. Is there any way to do it? And in which ...
3
votes
1answer
732 views

How to share an object between test methods?

I get System.NullPointerException: Attempt to de-reference a null object when I try to do the following: @isTest private class MyControllerTest { static MyController controller; @testSetup ...
3
votes
1answer
187 views

fixedSearchResults set in @testSetup method, but cannot access in testMethod

I'm new to salesforce, but have been given a task that I am puzzled on. I am given a test class with an @testSetup method which inserts relevant data for the test methods. In my test method, SOQL ...
3
votes
2answers
66 views

'List has no rows for assignment to SObject' Mystery

I have the following test class: @isTest private class PMTools_AppliedTechExtensionTest { @TestSetup static void SetupTestData(){ Account testAccount = new Account( ...
3
votes
1answer
1k views

Unit Testing ConnectApi.ChatterFeeds.getFeedElementBatch with auto-created feed posts

I have created a webservice APEX class that I need to write a Unit test for. The webservice assembles a custom filtered list of Chatter 'FeedElements'. All 'FeedElements' are system created, i.e. they ...
2
votes
2answers
221 views

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

I would like to use @TestSetup in my unit tests to create common data for each of my test methods: @isTest private class AwesomeTests { // how many awesome things to setup static final ...
2
votes
3answers
335 views

Use ANT to execute existing ApexTests [closed]

I would like to periodically execute local tests in an org outside of a package deployment. I have a deploy entry in my build.xml file that sets testLevel="RunLocalTests", and has an entry for ...
2
votes
1answer
323 views

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

Assume you have 2 classes, A and B, each with 10 methods of equal length. You write 2 test classes, TestA and TestB, each running all 10 methods and producing 100% coverage. However, when run from ...
2
votes
1answer
941 views

Test.startTest() in testSetup method

An year ago I asked a question regarding the advantages of using testSetUp annotation instead of having a method for common data and calling it in every test method. My main issue at the time with ...
2
votes
1answer
3k views

regarding External entry point

i have one custom field called Unique_Vendor_Id__c on account object (external id checkbox checked) and its auto-number. now in test class when i create account for test data Unique_Vendor_Id__c ...
2
votes
2answers
195 views

How do I read the Id for an object instantiated by my @testSetup method?

I've setup a @testSetup method to share amongst my Unit Test methods. One of my methods relies on the auto-incremented Id of the object created by my test method. Here's my code: @testSetup static ...
2
votes
2answers
87 views

Accessing Variables from a Parent Class

I am attempting to set up tests for my APEX classes, more specifically, I am attempting to create a parent class that includes a prepareDatabase function that creates data for my tests, and this ...
2
votes
3answers
192 views

How do I pass record IDs from testSetup to isTest methods?

I want to create data in a test class that can be used by multiple test methods. However, for the test method to be able to access that test data, I need a way to pass the record IDs to from the @...
2
votes
2answers
100 views

Best Practice for testing with RunAs method

I noticed that when you create a user in a test class that the username must be unique in the real production environment. Most of my test classes are creating a user to then run the test method as. ...
2
votes
1answer
962 views

use testsetup for common methods in test class

As per my understanding and salesforce documentation, class annotated with @isTest should contain "only test methods". But there are some situations where we might need to have utility methods and ...
2
votes
1answer
1k views

Create test data with more than 10000 rows dynamically in apex

I am trying to Create test data with more than 10000 rows dynamically in a test class. Am hitting the dml row limit error (Too Many DML Rows: 10001). I dont want to use loaddata feature as I need to ...
2
votes
1answer
571 views

Test Class Data Setup - Governor Limits

The test method gets its set of governor limits, exclusive of the content between startTest and stopTest. I have faced the issue of 101 SOQL Governor Limits in test classes and mostly I have been able ...
2
votes
2answers
53 views

Regarding Apex class

While Executing Apex Class only Top Level Class should Be Declared as Static in Test Class,Why this Error is Occuring ,How to Avoid this Error? @isTest public class BusinessProfile1 { public void ...
2
votes
1answer
81 views

Test Lead has OwnerId but Cannot Access Owner Fields

I have the following test setup method: @TestSetup public static void testSetup() { //system.runAs(u) { //JITTriggerManagement.toggleTrigger('Lead'); Lead l = new Lead( ...
2
votes
1answer
326 views

Test Class failing in production [closed]

I have a test class failing while validating it and its base class deployment to production. The test setup is creating several records in 2 objects (1 standard 1 custom) and inserting them as a ...
2
votes
2answers
120 views

Am I setting up a Pricebook Entry incorrectly?

Update with More Code: The Test Class contains: Product2 plc = new Product2(Name='My Product', ProductCode='PC-1'); insert plc; Id pricebookId = Test.getStandardPricebookId()...
2
votes
1answer
40 views

Feedback on Test for Simple Trigger

I'm a newbie to Apex and can't say how much I appreciate this Board. I have a simple Trigger set up to update the Description fields on the Opportunity Object. I need to write a Test to cover the ...
2
votes
1answer
197 views

How do I Assert in a CanvasLifecycleHandler test class

I have a CanvasLifecycleHandler thst rewrites the url with a timeStamp. Is it possible to assert in the test class that the url has been changed? If so, how? CanvasLifecycleHandler public ...
2
votes
0answers
44 views

Apex Batch Test - works if I populate only one account, fails in setup with 2 accounts

I have written my first Apex batch processing class. It works when I manually create records and run it in my sandbox. It is very simple. We've added a set of fields that can have up to 3 values: '...
2
votes
0answers
154 views

Unit Test data setup is hitting Apex CPU time limit

I have written a set of methods in a test class that can be called to create and insert relational test data. I am using Test.StartTest() and Test.StopTest() to keep the operations for the test in ...
1
vote
1answer
587 views

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

I have a class I want to test that has an associated test class with it. I am wondering if I use a TestDataFactory class if the @TestSetup annotation will allow me to access the data inserted from my ...
1
vote
1answer
238 views

Attempt to de-reference a null object when calling class method from Test class

I am trying to write a test method that calls a method in my class to get some configuration data. The method in my class returns a Map. From my test method, when I call the method in my class, I ...
1
vote
2answers
74 views

How many records can be created in TestSetup method in test class?

How many records can be created in Test Setup method in test class
1
vote
1answer
518 views

How to write test class for DTO

I have a DTO class where lots of inncer classes are defined like..if i am wiring public OuterClass{ innerClass1{ //variables declarations } innerClass2{ //variables declarations } ... ... ...