CASE 1:
==>seeAllData=true
If we are defining a test class with @isTest(SeeAllData=true) then we can access data in test class from database under all method present in test class.Under this case annoting a method with @isTest(SeeAllData=false) would be ignored and you can access database data inside this method as well.
Let's understand with an example.
@isTest(SeeAllData=true)
public class testclassexample {
static testmethod void testMethodFirst() {
contact obj = [SELECT Id, Name from contact where name='test' LIMIT 1];
//Accessing contact from database
}
@isTest(SeeAllData=false)
static testmethod void testMethodFirst() {
contact obj1 = [SELECT Id, Name from contact where name='test' LIMIT 1];
//Accessing contact from database although @isTest(SeeAllData=false) is applied for method but it will get ignored
as test class is having @isTest(SeeAllData=true).
}
}
If we are defining a test class with @isTest(SeeAllData=true) then we can access data in test class from database under all method present in test class.Under this case annoting a method with @isTest(SeeAllData=false) would be ignored and you can access database data inside this method as well.
Let's understand with an example.
@isTest(SeeAllData=true)
public class testclassexample {
static testmethod void testMethodFirst() {
contact obj = [SELECT Id, Name from contact where name='test' LIMIT 1];
//Accessing contact from database
}
@isTest(SeeAllData=false)
static testmethod void testMethodFirst() {
contact obj1 = [SELECT Id, Name from contact where name='test' LIMIT 1];
//Accessing contact from database although @isTest(SeeAllData=false) is applied for method but it will get ignored
as test class is having @isTest(SeeAllData=true).
}
}
Case 2:
==>@isTest(SeeAllData=false)
If we are defining a test class with @isTest(SeeAllData=false) then we cannot access data in test class from database under all method present in test class.Under this case annoting a method with @isTest(SeeAllData=true) would not be ignored and you can access database data inside this method.
Let's understand with an example.
@isTest(SeeAllData=false)
public class testclassexample {
static testmethod void testMethodFirst() {
//You cannot access like this now.
contact obj = [SELECT Id, Name from contact where name='test' LIMIT 1];
//You need to create test data
Contact obj=new contact();
obj.name='test';
insert obj;
}
@isTest(SeeAllData=true)
static testmethod void testMethodFirst() {
contact obj1 = [SELECT Id, Name from contact where name='test' LIMIT 1];
//Accessing contact from database although @isTest(SeeAllData=false) is applied for class but it will get ignored.
}
}
If we are defining a test class with @isTest(SeeAllData=false) then we cannot access data in test class from database under all method present in test class.Under this case annoting a method with @isTest(SeeAllData=true) would not be ignored and you can access database data inside this method.
Let's understand with an example.
@isTest(SeeAllData=false)
public class testclassexample {
static testmethod void testMethodFirst() {
//You cannot access like this now.
contact obj = [SELECT Id, Name from contact where name='test' LIMIT 1];
//You need to create test data
Contact obj=new contact();
obj.name='test';
insert obj;
}
@isTest(SeeAllData=true)
static testmethod void testMethodFirst() {
contact obj1 = [SELECT Id, Name from contact where name='test' LIMIT 1];
//Accessing contact from database although @isTest(SeeAllData=false) is applied for class but it will get ignored.
}
}
Thanks for the explanation .
ReplyDeleteNicely explained, Thanks
ReplyDelete