Play Games

Search This Blog

Wednesday, July 26, 2017

How to write test classes in Salesforce

Step 1:Test class starts with @isTest and then we create test class as shown below

@isTest
public class AccountControllerTest {
}

Note: 1) While naming test class,We may append/prepend Test for the original class/controller.

2) If you perform any DML operations like insert/update/delete,those data are not saved to Original Database.You won't be able to access those test data in normal classes.

@isTest : indicates that the class is a test class.

Step 2: Create one method to validate/test data present in the original controller.

@isTest
public class AccountDisplayTest {
//Sample method to test data of original class
public static testMethod void validateData() {

}
}

Test.StartTest() and Test.StopTest() are used to indicate exact start and stop of test class.Use them inside the method as shown below after setting up of data.

@isTest
public class AccountDisplayTest {
//Sample method to test data of original class
public static testMethod void validateData() {
Test.StartTest();
Test.StopTest();
}
}

Step 3: Call the methods present in the original controller.
Example:
Assume we have the following class with 4 methods as shown below.
public class AccountController {
public void method1() {
}
public void method2() {
}
public void method3() {
}
public void method4() {
}
}
In order to call the methods of above class,We create instance of that class first and then we call those methods one by one.
like this
AccountController objAccount = new AccountController();
objAccount.method1();
objAccount.method2();
objAccount.method3();
objAccount.method4();

The test class will look like this

@isTest
public class AccountDisplayTest {
//Sample method to test data of original class
public static testMethod void validateData() {
Test.StartTest();
AccountController objAccount = new AccountController();
objAccount.method1();
objAccount.method2();
objAccount.method3();
objAccount.method4();
Test.StopTest();
}
}

Step 4: If a class contains static methods,directly call those methods with class name without creating instance to that class.
Example:
Assume we have the following class with 2 methods as shown below.
public class AccountController {
public static void staticMethod1() {
}
public static void staticMethod2() {
}
}
In order to call the methods of above class,We directly call those methods by using className
like this
AccountController.staticMethod1();
AccountController.staticMethod2();

The test class will look like this
@isTest
public class AccountDisplayTest {
//Sample method to test data of original class
public static testMethod void validateData() {
Test.StartTest();
AccountController.staticMethod1();
AccountController.staticMethod2();
Test.StopTest();
}
}

Step 5: If a method in a class has any arguments,then while calling that method in test class,We need to pass respective arguments.
Example:
Assume we have the following class with 2 methods as shown below.
public class AccountController {
public static void staticMethod1(Account acc) {
}
public static void staticMethod2(Account[] lstAcc) {
}
}

In order to call the methods of above class,first we create data that is required and then we call those methods by passing valid arguments.
For staticMethod1,Account acc is argument.So,first we insert account and then we can call that method by passing account instance like this
Account objAccount = new Account();
objAccount.name ='Test Account';
insert objAccount;
AccountController.staticMethod1(objAccount);

The test class will look like this
@isTest
public class AccountDisplayTest {
//Sample method to test data of original class
public static testMethod void validateData() {
Test.StartTest();
Account objAccount = new Account();
objAccount.name ='Test Account';
insert objAccount;
AccountController.staticMethod1(objAccount);
Test.StopTest();
}
}
For staticMethod2,Account[] is the argument.It means list of Accounts are passed as argument to that method.So first we need to prepare that accountList and then call that method by passing that list variable as shown below.
List<Account> lstAccount = new List<Account>();
Account objAccount1 = new Account();
objAccount1.name ='Test Account1';
lstAccount.add(objAccount1);
Account objAccount2 = new Account();
objAccount2.name ='Test Account2';
lstAccount.add(objAccount2);
insert lstAccount;
AccountController.staticMethod2(lstAccount);
The test class will look like this
@isTest
public class AccountDisplayTest {
//Sample method to test data of original class
public static testMethod void validateData() {

List<Account> lstAccount = new List<Account>();
Account objAccount1 = new Account();
objAccount1.name ='Test Account1';
lstAccount.add(objAccount1);
Account objAccount2 = new Account();
objAccount2.name ='Test Account2';
lstAccount.add(objAccount2);
insert lstAccount;
Test.StartTest();
AccountController.staticMethod2(lstAccount);
Test.StopTest();
}
}
Note: More info will be provided soon.....

No comments:

Post a Comment