Interview Questions: Part-4

1. Is it possible to use "SeeAllData =true" for any non-Test methods inside the Test class if the Test class has @TestSetup annotation method?
Answer: Test class containing a "TestSetup" annotated methods will not support to have any TestMethod with  @isTest(SeeAllData=true) annotation. Below class will not even save.
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
@isTest
Public class BudgetAllocationReturnTest
{
    @isTest(seeAllData=true)
    Static void getCustomSettingData()
    {
        List<YourCustomSetting__c> val = YourCustomSetting.getall().values();
        System.debug('----val--- '+val);

    }
    @TestSetup 
    Static void dataGeneration()
    {
  //Data creation logic here 
 }
}

2. Governor limits for each TestMethod will reset?
Answer: Yes, for each Test method, we will get fresh governor limits. To prove this, I have implemented the below Program.
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
@isTest
public class LearningTestClass 
{
    Public static TestMethod void m1()
    {
        System.debug('------ 1st Method ----- \n');
        System.debug('Before Rows --- '+Limits.getDMLRows());
        System.debug('Before Total Rows allowed --- '+Limits.getLimitDMLRows());
        System.debug('Before DML statements --- '+Limits.getDMLStatements());
        System.debug('Before Total DML Statements --- '+Limits.getLimitDMLStatements());
        for(Integer i=0; i<149; i++)
        {
            Account acc = new Account(Name='Mannjunath');
            Insert acc;
        }
        System.debug('------ 1st Method ----- \n');
        System.debug('After Rows --- '+Limits.getDMLRows());
        System.debug('After Total Rows allowed --- '+Limits.getLimitDMLRows());
        System.debug('After DML statements --- '+Limits.getDMLStatements());
        System.debug('After Total DML Statements --- '+Limits.getLimitDMLStatements());
    }
    Public static TestMethod void m2()
    {
        System.debug('------ 2nd Method -----');
        System.debug('Before Rows --- '+Limits.getDMLRows());
        System.debug('Before Total Rows allowed --- '+Limits.getLimitDMLRows());
        System.debug('Before DML statements --- '+Limits.getDMLStatements());
        System.debug('Before Total DML Statements --- '+Limits.getLimitDMLStatements());
        for(Integer i=0; i<149; i++)
        {
            Account acc = new Account(Name='Mannjunath');
            Insert acc;
        }
        System.debug('------ 2nd Method -----');
        System.debug('After Rows --- '+Limits.getDMLRows());
        System.debug('After Total Rows allowed --- '+Limits.getLimitDMLRows());
        System.debug('After DML statements --- '+Limits.getDMLStatements());
        System.debug('After Total DML Statements --- '+Limits.getLimitDMLStatements());        
    }
    Public static TestMethod void m3()
    {
        System.debug('------ 3rd Method ----- ');
        System.debug('Before Rows --- '+Limits.getDMLRows());
        System.debug('Before Total Rows allowed --- '+Limits.getLimitDMLRows());
        System.debug('Before DML statements --- '+Limits.getDMLStatements());
        System.debug('Before Total DML Statements --- '+Limits.getLimitDMLStatements());
        for(Integer i=0; i<149; i++)
        {
            Account acc = new Account(Name='Mannjunath');
            Insert acc;
        }
        System.debug('------ 3rd Method ----- ');
        System.debug('After Rows --- '+Limits.getDMLRows());
        System.debug('After Total Rows allowed --- '+Limits.getLimitDMLRows());
        System.debug('After DML statements --- '+Limits.getDMLStatements());
        System.debug('After Total DML Statements --- '+Limits.getLimitDMLStatements());
    }    
}
After executing the above test class results are :
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
USER_DEBUG|[6]|DEBUG| ------ 1st Method ----- 
USER_DEBUG|[7]|DEBUG| Before Rows --- 0
USER_DEBUG|[8]|DEBUG| Before Total Rows allowed --- 10000
USER_DEBUG|[9]|DEBUG| Before DML statements --- 0
USER_DEBUG|[10]|DEBUG| Before Total DML Statements --- 150

USER_DEBUG|[16]|DEBUG| ------ 1st Method ----- 
USER_DEBUG|[17]|DEBUG| After Rows --- 149
USER_DEBUG|[18]|DEBUG| After Total Rows allowed --- 10000
USER_DEBUG|[19]|DEBUG| After DML statements --- 149
USER_DEBUG|[20]|DEBUG| After Total DML Statements --- 150

USER_DEBUG|[24]|DEBUG| ------ 2nd Method -----
USER_DEBUG|[25]|DEBUG| Before Rows --- 0
USER_DEBUG|[26]|DEBUG| Before Total Rows allowed --- 10000
USER_DEBUG|[27]|DEBUG| Before DML statements --- 0
USER_DEBUG|[28]|DEBUG| Before Total DML Statements --- 150

USER_DEBUG|[34]|DEBUG| ------ 2nd Method -----
USER_DEBUG|[35]|DEBUG| After Rows --- 149
USER_DEBUG|[36]|DEBUG| After Total Rows allowed --- 10000
USER_DEBUG|[37]|DEBUG| After DML statements --- 149
USER_DEBUG|[38]|DEBUG| After Total DML Statements --- 150

USER_DEBUG|[42]|DEBUG| ------ 3rd Method ----- 
USER_DEBUG|[43]|DEBUG| Before Rows --- 0
USER_DEBUG|[44]|DEBUG| Before Total Rows allowed --- 10000
USER_DEBUG|[45]|DEBUG| Before DML statements --- 0
USER_DEBUG|[46]|DEBUG| Before Total DML Statements --- 150

USER_DEBUG|[52]|DEBUG| ------ 3rd Method ----- 
USER_DEBUG|[53]|DEBUG| After Rows --- 149
USER_DEBUG|[54]|DEBUG| After Total Rows allowed --- 10000
USER_DEBUG|[55]|DEBUG| After DML statements --- 149
USER_DEBUG|[56]|DEBUG| After Total DML Statements --- 150
3. What are the precautions you will take while using "size()" or "isEmpty()" methods of "List" class?
Answer:  Below logic will fail with "System.NullPointerException: Attempt to de-reference a null object" error.
1
2
3
4
5
6
7
8
9
List<Account> accList;
if(accList.isEmpty())
{
    System.debug('--I am in isEmpty() method-- ');
}
if(accList.size())
{
    System.debug('--I am in size() method-- ');
}
Reason for failure: The list is not instantiated and no elements added to the List.
Solution: We all ways needs to instantiate[ Observe the Line#1 ] the "List" class like below.
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
List<Account> accList = new List<Account>();
if(accList.isEmpty())
{
    System.debug('--I am in isEmpty() method-- ');
 //Your DML operations or someother business logic.
}
if(accList.size())
{
    System.debug('--I am in size() method-- ');
 //Your DML operations or someother business logic.
}

tHiNk gooD and dO thE bEsT.........MANJU NATH 🌝

Comments

Post a Comment