Slaesforce FAQ

how to load test data from static resource in salesforce

by Ryley Bernier Published 2 years ago Updated 2 years ago
image

Loading Test Data Using the Test.loadData method, you can populate data in your test methods without having to write many lines of code. Simply, add the data in a.csv file, create a static resource for this file, and then call Test.loadData within your test method by passing it the sObject type token and the static resource name.

Full Answer

How to load data from a static resource in a test?

Call Test.loadData within your test method and passing it the sObject type token and the static resource name. The Test.loadData method returns a list of sObjects that correspond to each record inserted.

What are static resources in Salesforce?

The static resource is a comma-delimited file ending with a.csv extension. The file contains field names and values for the test records. The first line of the file must contain the field names and subsequent lines are the field values. To learn more about static resources, see “Defining Static Resources” in the Salesforce online help.

How do I test a callout from a static resource?

Apex provides the built-in StaticResourceCalloutMock class that you can use to test callouts by specifying the response body in a static resource. When using this class, you don’t have to provide your own implementation of the HttpCalloutMock interface.

How do I add a static resource to an application?

If you specify a content type of application/json for the response, the file content should be a JSON string, such as {"hah":"fooled you"}. From Setup, enter Static Resources in the Quick Find box, then select Static Resources. Click New. Name your static resource. Choose the file to upload. Click Save.

image

How do I load test data in Salesforce?

Add the data in a . csv file. Create a static resource for this file. Call Test....Test. loadData ExampleFrom Setup, enter Static Resources in the Quick Find box, then select Static Resources.Click New.Name your static resource testAccounts .Choose the file you created.Click Save.

How do you use a static resource in Apex test class?

Create test data in csv files for each object as independent records. Upload the files as static resources in my sandbox. Inside my test Apex class load the Accounts static resource. Then, load the opportunities static resource and after loading them, set the Accounts as parents and update the opportunities.

How do I read a CSV file from a static resource in salesforce?

Load data stored in Static ResourcesStep 1: Create CSV files with your data and upload them to Static Resources. ... Step 2: Create a CSV Reader class. ... Step 3: Adapt and run the following Anonymous Script on your Sandbox.

What is test isRunningTest () in Salesforce?

isRunningTest() - Returns true if the currently executing code was called by code contained in a test method, false otherwise. Use this method if you need to run different code depending on whether it was being called from a test.

How do you call a static resource in Test class?

Call Test. loadData within your test method and passing it the sObject type token and the static resource name....Test. loadData ExampleFrom Setup, enter Static Resources in the Quick Find box, then select Static Resources.Click New.Name your static resource testAccounts .Choose the file you created.Click Save.

How do I reference a static resource in salesforce?

You can reference a static resource in page markup by name using the $Resource global variable instead of hard-coding document IDs:To reference a standalone file, use $Resource. < ... To reference a file in an archive, use the URLFOR function.

How do you upload a static resource?

Create and Upload a Simple Static ResourceFrom Setup, enter Static Resources in the Quick Find box, then select Static Resources, and then click New.Enter jQuery for the Name.Click Choose File, and then choose the jQuery JavaScript file you downloaded previously.If you see the Cache Control menu, choose Public.More items...

What is the use of test startTest and test stopTest?

Marks the point in your test code when your test actually begins. Use this method when you are testing governor limits. You can also use this method with stopTest to ensure that all asynchronous calls that come after the startTest method are run before doing any assertions or testing.

How do you run a single test method in a test class in Salesforce?

Set up a test run in the Developer Console to execute the test methods in one or more test classes. In the Developer Console, click Test | New Run. To limit how many tests can fail before your run stops, click Settings. Enter a value for Number of failures allowed , and then click OK.

How do I debug a test class in Salesforce?

Go to Setup>Developer>Apex Test Execution>Select Tests> pick the testing class you want to see the debug logs from can click run.

Testing HTTP Callouts Using StaticResourceCalloutMock

Apex provides the built-in StaticResourceCalloutMock class that you can use to test callouts by specifying the response body in a static resource. When using this class, you don’t have to provide your own implementation of the HttpCalloutMock interface.

Testing HTTP Callouts Using MultiStaticResourceCalloutMock

Apex provides the built-in MultiStaticResourceCalloutMock class that you can use to test callouts by specifying the response body in a static resource for each endpoint. This class is similar to StaticResourceCalloutMock except that it allows you to specify multiple response bodies.

Question

In an org with Person Accounts enabled, it possible to use Test.loadData () to load test Account records from a .csv file as Person Accounts (not as Business Accounts), but without hardcoding their RecordTypeIds in the CSV?

Attempt 1: Loading the Account records from a CSV with only the following three columns: Id, FirstName, LastName

When I did this, I was unable to replicate the behavior detailed in Inserting person accounts in test mode in which Salesforce automatically knows what type of Account to create based on what fields are populated.

Attempt 2: Loading the Account records from a csv with no RecordTypeIds, then later dynamically assigning RecordTypeIds through Apex

In addition to the Account CSV (with four columns, including Name, since I can't get around the Name required field error), I loaded Contacts from a CSV with columns: Id, AccountId, FirstName, LastName, such that each Contact only looked up to one Account, and no Contact looked up to the same Account.

Attempt 3: Adding the IsPersonAccount column to the CSV, with values true

The loaded records had IsPersonAccount set to true, but they were created with a RecordTypeId corresponding to Business Accounts.

image
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z 1 2 3 4 5 6 7 8 9