Spring ’15 – New Feature Added – @testSetup
One of the very interesting feature added in Spring ’15 is @testSetup. In this post I will try to explain with example more about the feature. Below is what you will get from release note about @testSetup.Now let’s check with example -Below is the class/methods for which we need to write test methods – public class AccountHelper { public List<Account> getAllAccounts(){ List<Account> allAccounts = [Select Id from Account]; return allAccounts; } public Integer getEmployeeCountFrom(String accountNumber){ Account fetchedAccount = [Select Id, NumberOfEmployees from Account where AccountNumber = :accountNumber LIMIT 1]; return fetchedAccount.NumberOfEmployees; } public List<Case> getAllCases(Id accountId){ return [select ID from Case where AccountId = :accountId]; } public void updateEmployeeCountFor(String accountNumber, Integer newEmployeeCount){ Account fetchedAccount = [Select Id, NumberOfEmployees from Account where AccountNumber = :accountNumber LIMIT 1]; fetchedAccount.NumberOfEmployees = newEmployeeCount; update fetchedAccount; }}Previously we used to write test classes like below -@isTestpublic class AccountHelperTest { static testMethod void testGetAllAccounts(){ List<Account> accounts = new List<Account>(); for(Integer i=0;i < 100;i++){ accounts.add(new Account(Name = ‘Universal Container’)); } insert accounts; Test.startTest(); AccountHelper accountHelper = new AccountHelper(); List<Account> allAccounts = accountHelper.getAllAccounts(); Test.stopTest(); System.assertEquals(100, allAccounts.size()); } static testMethod void testGetEmployeeCountFrom(){ Account anAccount = new Account(); anAccount.Name = ‘Universal Container’; anAccount.AccountNumber = ‘TEST’; anAccount.NumberOfEmployees = 100; insert anAccount; Test.startTest(); AccountHelper accountHelper = new AccountHelper(); Integer numberOfEmployees = accountHelper.getEmployeeCountFrom(‘TEST’); Test.stopTest(); System.assertEquals(100, numberOfEmployees); } static testMethod void testGetAllCases(){ //Create the account first Account anAccount = new Account(); anAccount.Name = ‘Universal Container’; insert anAccount;...
Read More