Test Class Questions
Test Class Questions
Test Class Questions
.When we are moving code from sandbox to production org we need to write a test class.
.Whatever code we have written we need to ensure that each and every condition is
correct
Points to remember.
1)Test class does not have access to organization data but if we write
@isTest(seeAllData=true),
2)As every organization have code limit by writing @isTest above test class, the limit will
not be considered.
3)Every test class has a method declared with testmethod keyword and is static.
Syntax:
.Whatever test we are performing we write the test inside of Test.starttest() and
Test.stoptest.
.Each test method can call the start test and stop test only once.
3) What is the use of seeAllData=true ?
==>seeAllData=true
If we are defining a test class with @isTest(SeeAllData=true) then we can access data in
test class from the database under all method present in the test class. Under this case
annotating a method with @isTest(SeeAllData=false) would be ignored and you can
access database data inside this method as well.
@isTest(SeeAllData=true)
contact obj = [SELECT Id, Name from contact where name='test' LIMIT 1];
@isTest(SeeAllData=false)
contact obj1 = [SELECT Id, Name from contact where name='test' LIMIT 1];
==>@isTest(SeeAllData=false)
If we are defining a test class with @isTest(SeeAllData=false) then we cannot access data
in test class from the database under all method present in the test class. Under this case
annotating a method with @isTest(SeeAllData=true) would not be ignored and you can
access database data inside this method.
contact obj = [SELECT Id, Name from contact where name='test' LIMIT 1];
obj.name='test';
insert obj;
@isTest(SeeAllData=true)
contact obj1 = [SELECT Id, Name from contact where name='test' LIMIT 1];
2) We Should test "test cases" for both positive and negative test scenario.
3) Avoid using (SeeAllData=true) in test class because it might happen that the test class
pass in sandbox but fails in production if data is not present in production.
6) What is @testSetup?
Method that are annotated with @testSetup are used to create test data and we can this
test data inside every method of the test class. The testSetup method is executed first in
any test class before other methods.
7) Let say i have a test class in which i have created a testSetup method in
which i am creating a contact record. Let say i have two test method inside
my test class which is accessing this contact record. If i access this contact
record in testmethod1() and made a update to the contact record from
testSetup method and now i am accessing this contact in testmethod2(), so
will i get the updated contact record or the original unmodified contact
record in testmethod2()?
testMethod2() will gets access to the original unmodified state of record, this is because
changes are rolled back after each test method finishes execution.
Using @TestVisible annotation before variable or methods or inner classes inside apex
class allows test class to access them. This annotation does not change the visibility of
variable or methods or inner classes if accessed by non-test class.
Apex class:
Test class:
@isTest
public class apexClassExampleTest{
Integer rollNo=apexClassExample.rollNo;
Test class runs in system mode, System.runAs() allow us to run the test in context of
current user where user 's record sharing is taken into account. User permission or field
level permission are not taken into account.
Sometimes it may happen that in test class you are not able to satisfy certain condition in
apex class
so under this case, we can bypass this condition by writing test.isrunningtest() in apex
class.
Syntax:
if(a=3 || test.isrunningtest())