@testSetup Annotation Sample

The Spring '15 Salesforce release introduced the concept of the @testSetup annotation. According to the documentation, methods defined with the @testSetup annotation are used for creating common test records that are available for all test methods in the class.

Until now any test data for unit testing had to be created within each test method even if the same data would work for multiple methods within the same test class. Developers would often copy and paste the data creation portion of the test method from a previous unit test and only tweak when needed.

With this new annotation developers can now have a single method within a test class that defines all of the test data to be used across all test methods within that same class. This should reduce the number of lines of code in your test class and it should reduce the processing time when the test class is run during deployments.

The method within the class with the @testSetup annotation is always run before any of the other methods in the class. This ensure that the data is created and available for each subsequent method within the class.

Below is an example of how to use this @testSetup annotation.

/*
	Created by: Greg Hacic
	Last Update: 31 July 2015 by Greg Hacic
	Questions?: greg@interactiveties.com
*/
@isTest
private class testSetupAnnotationUnitTestsClass {
	
	//illustrates how to grab a record from the test data and use it for testing a visualforce page extension
	static testMethod void unitTestVisualforceExtension() {
		Account a = [SELECT Id FROM Account WHERE Name = 'Unit Test Corp' LIMIT 1]; //grab the Account that was created in the allTheDataForThisTestClass method
		Test.startTest(); //denote testing context
		PageReference pageRef = Page.yourVisualforcePage; //create a page reference to yourVisualforcePage.page
		Test.setCurrentPage(pageRef); //set page context
		ApexPages.StandardController standardController = new ApexPages.standardController(a); //construct the standard account object controller
		yourVisualforcePageExtension pageExt = new yourVisualforcePageExtension(standardController); //construct the extension for the Visualforce page
		//test the methods from your controller...
		Test.stopTest(); //revert from testing context
	}
	
	//illustrates how to build more data into your unit test
	static testMethod void unitTestCreateMoreData() {
		List<Contact> contacts = [SELECT Id FROM Contact]; //grab the Contacts created in the allTheDataForThisTestClass method
		//create a Campaign
		List<Campaign> campaigns = new List<Campaign>();
		campaigns.add(new Campaign(Name = 'Unit Testing for Today Campaign', Description = 'This is for testing purposes only!'));
		insert campaigns;
		//create CampaignMembers
		List<CampaignMember> campaignmembers = new List<CampaignMember>(); //list for new CampaignMembers
		for (Contact c : contacts) { //for all contact records we already inserted
			campaignmembers.add(new CampaignMember(CampaignId = campaigns[0].Id, ContactId = c.Id, Status = 'Sent')); //create a new CampaignMember
		}
		insert campaignmembers; //insert the CampaignMembers
		Test.startTest(); //denote testing context
		//do your testing...
		Test.stopTest(); //revert from testing context
	}
	
	//testing data setup for all methods in this class
	@testSetup
	static void allTheDataForThisTestClass() {
		//create accounts
		List<Account> accounts = new List<Account>();
		accounts.add(new Account(Name = 'Unit Test Corp'));
		accounts.add(new Account(Name = 'Unit Test LLC'));
		accounts.add(new Account(Name = 'Parent Company Inc'));
		insert accounts;
		//update accounts in order to create a relationship
		List<Account> accountUpdates = new List<Account>();
		accountUpdates.add(new Account(Id = accounts[0].Id, ParentId = accounts[1].Id));
		accountUpdates.add(new Account(Id = accounts[1].Id, ParentId = accounts[2].Id));
		update accountUpdates;
		//create some contacts
		List<Contact> contacts = new List<Contact>();
		contacts.add(new Contact(AccountId = accounts[0].Id, FirstName = 'Tess', LastName = 'Dachshund'));
		contacts.add(new Contact(AccountId = accounts[0].Id, FirstName = 'Grachus', LastName = 'Dachshund'));
		contacts.add(new Contact(AccountId = accounts[1].Id, FirstName = 'Pete', LastName = 'Dachshund'));
		contacts.add(new Contact(AccountId = accounts[1].Id, FirstName = 'Alphonse', LastName = 'Dachshund'));
		insert contacts;
		//create some Opportunities
		List<Opportunity> opportunities = new List<Opportunity>();
		opportunities.add(new Opportunity(AccountId = accounts[0].Id, CloseDate = Date.Today().addMonths(1), Name = 'Testing Opportunity - This is a truck', StageName = '1. Identified Opportunity'));
		opportunities.add(new Opportunity(AccountId = accounts[1].Id, CloseDate = Date.Today().addMonths(1), Name = 'Testing Opportunity - This is a weasel', StageName = '1. Identified Opportunity'));
		insert opportunities;
	}

}

The first test method labeled unitTestVisualforceExtension illustrates how to grab a record and use that record when constructing the standard controller for an object within a test method.

The second method labeled unitTestCreateMoreData illustrates how to create more data in addition to the data that was already created from the @testSetup annotated method. The additional test data created within this method is only available within this method itself.

You could have many more methods defined within this test class and all would use the data created via the @testSetup annotated method.

Automated Exchange Rates in Salesforce.com

Reduce Repetitive Tasks, Eliminate Errors & Free Up Your Administrators.

Birthday Reminders for Salesforce.com

It might lead to a sale. Or it might make you feel good.