1. S U B M I T T E D B Y :
S U R A J K U M A R S I N G H
S O F T W A R E D E V E L O P E R
I B I R D S S O F T W A R E S E R V I C E S P V T . L T D .
1/24/2016Ibirds Software Services Pvt. Ltd.
1
2. Contents
1/24/2016Ibirds Software Services Pvt. Ltd.
2
Introduction
What to Test in Test Classes?
Annotation in Test Classes
Use Of Static Resource
Importance of System.Assert
Use Of Test.StartTest/StopTest Methods
How To Write
Utility Test Class
Deployment
Points to Remember
3. Introduction
1/24/2016Ibirds Software Services Pvt. Ltd.
3
Ways of Testing : Only Through Interface.
Why Test Class? -- TRUST
Features Of test Classes
While testing it does not made any changes to the real database.
If your tests access organization data, this prevents accidental
deletions or modifications to existing records.
No requirement for test data cleanup
Test methods don't commit data. So We Do not find any track
record.
4. What To Test?
1/24/2016Ibirds Software Services Pvt. Ltd.
4
Single action and Bulk Action(upto 200)
“Null Pointer Exception test” (min 1).
Positive Behavior (Expected Result).
Negative Behavior(Acquire expected error).
Restricted user
Use runAs Method
runAs(user):
In Normal ways permissions and record sharing of the current user are
not taken into account.
runAs(System.Version)
5. Annotation in Test Class
1/24/2016Ibirds Software Services Pvt. Ltd.
5
@isTest :
To Identify a Test Class.
@IsTest(SeeAllData=true) :
Open up data access to records in your organization.
@isTest(SeeAllData=false) :
For methods to who do not use Organization's Data.
@TestVisible :
Use to access private members and methods of testing class inside
Test Class.
@TestVisible private static Integer recordNumber = 1; // in Class.
Integer i = class.recordNumber; // in Test Classs.
6. Annotation in Test Class
1/24/2016Ibirds Software Services Pvt. Ltd.
6
@testSetup :
Prerequisite data for all test methods, or a common set of records that are available to
test methods
Save Time – Need not to re-create record for each test Method.
By Priority, this method have highest priority.
Any changes(DML Operations) are not affected in between test methods.
@testSetup static void methodName() {}
Note:
If @isTest(SeeAllData=true) is in use, Then it will be failed.
If Multiple test setup methods are used in a test class, then in Order of execution
can’t be guaranteed.
Any Exception will fails the whole test class.
If a test setup method calls a non-test method of another class, no code coverage
is calculated for the non-test method.
7. Importance of System.Assert
1/24/2016Ibirds Software Services Pvt. Ltd.
7
assert(condition, msg):
Check the specified condition is true. If it is not, a error message is returned
that causes code execution to halt.
assertEquals(expected, actual, msg) :
Check the first two arguments are the same. If they are not, a error message
is returned that causes code execution to halt.
assertNotEquals(expected, actual, msg) :
Check the first two arguments are different. If they are the same, a
error message is returned that causes code execution to halt.
8. Start/Stop Test
1/24/2016Ibirds Software Services Pvt. Ltd.
8
Test.StartTest():
Marks the point in your test code when your test actually begins.
Test.StopTest():
The stopTest method marks the point in your test code when your test ends.
Each test method is allowed to call this method only once.
All of the code before this method should be used to
initialize variables, populate data structures, and so on,
allowing you to set up everything you need to run your test.
Assigned a new set of governor limits.
When stopTest is executed, all asynchronous processes are
run synchronously.
9. How to write Test Classes
1/24/2016Ibirds Software Services Pvt. Ltd.
9
For Batch Class:
Databese.execute() line must be in between Test.startTest() and Test.stopTest().
For Standard Controller Class:
10. How to write Test Classes
1/24/2016Ibirds Software Services Pvt. Ltd.
10
For Schedule Class:
Method must be in between Test.startTest() and Test.stopTest().
11. Use Of Static Resource in Test Classes
1/24/2016Ibirds Software Services Pvt. Ltd.
11
Test.loadData: Populate data in your test methods without having to
write many lines of code
Use CSV File.
12. Common Test Utilities
1/24/2016Ibirds Software Services Pvt. Ltd.
12
They Are Public test classes that contain reusable code for test data creation.
Defined with the @isTest annotation to exclude from organization’s code size
limit.
A class that contains all necessary information for creation of test object and
have values to full fill all validation rules.
13. Deployment
1/24/2016Ibirds Software Services Pvt. Ltd.
13
At least 75% of your Apex code must be covered.
When deploying Apex, each unit test in organization is executed by
default.
System.debug, Test methods and test classes are not counted as part
of Apex code coverage.
While only 75% of your Apex code must be covered by tests, your focus
shouldn't be on the percentage of code that is covered. Instead, you
should make sure that every use case of your application is covered,
including positive and negative cases, as well as bulk and single
records. This should lead to 75% or more of your code being covered by
unit tests.
14. Points To Remember
1/24/2016Ibirds Software Services Pvt. Ltd.
14
All test methods should reside in a separate class.
@isTest annotation at Beginning of class.
Use Test.isRunningTest().
A method without an assert statement is not considered a
test method.
Use of Comments
Do Not Use @isTest(SeeAllData = true) unless required(e.g
Products ).
Use Test.runAs(user).
Use Test Utility Class.