One of the very interesting feature added in Spring ’15 is
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;
}
}
@isTest
public 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;
//Create the cases now
List<Case> cases = new List<Case>();
for(Integer i=0;i < 10;i++){
cases.add(new Case(
Status = 'New',
Priority = 'Medium',
AccountId = anAccount.Id,
Origin = 'Phone'
));
}
insert cases;
Test.startTest();
AccountHelper accountHelper = new AccountHelper();
List<Case> allCases = accountHelper.getAllCases(anAccount.id);
Test.stopTest();
System.assertEquals(10, allCases.size());
}
}
@isTest
public class ApexHelperTestAdvanced {
@testSetup static void setup() {
// Create common test accounts
List<Account> accounts = new List<Account>();
for(Integer i=0;i < 100;i++){
accounts.add(new Account(Name = 'Universal Container'));
}
//Add few account details to one Account
accounts[0].AccountNumber = 'TEST';
accounts[0].NumberOfEmployees = 100;
//Insert Account
insert accounts;
//Add Case Details
List<Case> cases = new List<Case>();
for(Integer i=0;i < 10;i++){
cases.add(new Case(
Status = 'New',
Priority = 'Medium',
AccountId = accounts[0].Id,
Origin = 'Phone'
));
}
//Insert cases
insert cases;
}
static testMethod void testGetAllAccounts(){
Test.startTest();
AccountHelper accountHelper = new AccountHelper();
List<Account> allAccounts = accountHelper.getAllAccounts();
Test.stopTest();
System.assertEquals(100, allAccounts.size());
}
static testMethod void testGetEmployeeCountFrom(){
Test.startTest();
AccountHelper accountHelper = new AccountHelper();
Integer numberOfEmployees = accountHelper.getEmployeeCountFrom('TEST');
Test.stopTest();
System.assertEquals(100, numberOfEmployees);
}
static testMethod void testGetAllCases(){
//Fetch the account ID
Account fetchedAccount = [Select Id from Account where AccountNumber = 'Test'];
Test.startTest();
AccountHelper accountHelper = new AccountHelper();
List<Case> allCases = accountHelper.getAllCases(fetchedAccount.id);
Test.stopTest();
System.assertEquals(10, allCases.size());
}
static testMethod void testUpdateEmployeeCountFor(){
Test.startTest();
AccountHelper accountHelper = new AccountHelper();
accountHelper.updateEmployeeCountFor('TEST',200);
Integer numberOfEmployees = accountHelper.getEmployeeCountFrom('TEST');
Test.stopTest();
System.assertEquals(200, numberOfEmployees);
}
static testMethod void testRollBackFunctionality(){
Test.startTest();
AccountHelper accountHelper = new AccountHelper();
Integer numberOfEmployees = accountHelper.getEmployeeCountFrom('TEST');
Test.stopTest();
System.assertEquals(100, numberOfEmployees);
}
}