function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
JBabuJBabu 

Why I am not able to see static data in test method

Hi,

 

Under "Static" I have created a user and tried to use that user in the static test method. When I am  debugging I dont see an values of the user record which I have created in the static declaration.

 

1  @isTest(seeAllData=true)
2  public class TestBiggerRevAccounts{
3
4  private static List<Account> testAccountsList;
5  static User sysAdminUser;
6  private static User userdata;
7  private static final Integer acct_cnt = 10;
8
9   static {
10      sysAdminUser = [select Id from User
11          where Profile.Name='System Administrator' and IsActive = true
12           limit 1];
13                   
14   System.runAs(sysAdminUser) {
15    userRole userroledata = new UserRole(Name = 'Test Executive');
16     insert userroledata;
17
18     Profile profiledata = [select Id FROM profile where name='System Administrator'];
19
20     userdata = new User(alias = 'user1', userroleid = userroledata.Id, email='testroleuser@test.com',                            emailencodingkey='UTF-8', lastname='Testing', languagelocalekey='en_US',
21                              localesidkey='en_US', profileid = profiledata.Id, country='United States',
22                              timezonesidkey='America/Las_Angeles', username='testroleuser@test.com');
23                              
24     insert userdata;  
25     system.debug('*** user data' +userdata);
26    }
27   }
28   
29  public static testMethod void BiggerRevAccountsUnitTest()
30    {        
31     System.runAs(sysAdminUser) {
32     
33    testAccountsList = datacreatormethod.createAccounts(acct_cnt);                                                    
34   
35     Test.StartTest();       
36      system.debug('*** user details' +userdata);
37     User user = [select  Id, Mybigger1__c from User  where   Id = :userdata.Id];   
38       
39     user.Mybigger1__c = 'testlist1';
40     update user;   
41      
42       Test.StopTest();    
43        
44        PageReference BiggerRevAccountsPage = Page.BiggerRevAccounts;
45        Test.setCurrentPage(BiggerRevAccountsPage);
46        ApexPages.Standardcontroller accountController = new ApexPages.Standardcontroller(new Account());   
47    
48    BiggerAccountExt extension = new BiggerAccountExt(accountController);   
49        extension.save();   
50        
51       }
52     }  
53
54  }

 

I am not able to see any debug statement mentioned in line 25 output in the log. I am not sure why this record is not getting inserted.

Because of that debug log of line 36 is displaying output as "null" .

This is causing an issue in line 37 : "Attempt to de-reference a null object".

 

Please let me know what is the issue.

 

Thanks,

JBabu.

 

 

 

 

thedabblerthedabbler

userdata will not be inserted because you have typo in the time zone key.That's invalid.

When I ran your user creation code I saw this in my log:

 

17:07:51.057 (3057307000)|VF_PAGE_MESSAGE|Time Zone: bad value for restricted picklist field: America/Las_Angeles
17:07:51.057 (3057436000)|EXCEPTION_THROWN|[23]|System.DmlException: Insert failed. First exception on row 0; first error: INVALID_OR_NULL_FOR_RESTRICTED_PICKLIST, Time Zone: bad value for restricted picklist field: America/Las_Angeles: [TimeZoneSidKey]

 

See you have Las_Angeles . I changed it to America/Los_Angeles and it inserted.

 

 

 

JBabuJBabu

Hi,

 

Even though I need to see debug statement output

 

*** user data  as null.

 

I corrected the location but I am not able to see the output statement of debug log  atleast ***userdata (also, I am not sure why the block under static method is not read).

 

Thanks,

JBabu.

JBabuJBabu

Are you able to see the debug log output of line 25?

thedabblerthedabbler

Yes after fixing the spelling of the time zone key the that debug  shows up in the log.

 

Here it is:In my case it is line 24.When the record was not inserted because of the spelling mistake that log wouldn't shouw up as it would break the esecution of the next satement once the exception occured on your dml.There may be some other error happening there which is preventing the logic to reach your debug line.That's only thing I can think of. 

 

20:11:18.141 (5141701000)|DML_END|[23]
20:11:18.142 (5142668000)|USER_DEBUG|[24]|DEBUG|*** user dataUser:{UserRoleId=00EZ0000000Hbb6MAC, EmailEncodingKey=UTF-8, Alias=user1, Email=testroleuser@test.com, TimeZoneSidKey=America/Los_Angeles, LanguageLocaleKey=en_US, Username=testroleuser@test.com, Id=005Z0000000JlfsIAC, LocaleSidKey=en_US, ProfileId=00eG00000013lksIAA, LastName=Testing, Country=United States}
20:11:18.150 (5150696000)|METHOD_EXIT|[2]|TestBiggerRevAccounts
20:11:18.156 (5156085000)|USER_DEBUG|[34]|DEBUG|*** user detailsUser:{UserRoleId=00EZ0000000Hbb6MAC, EmailEncodingKey=UTF-8, Alias=user1, Email=testroleuser@test.com, TimeZoneSidKey=America/Los_Angeles, LanguageLocaleKey=en_US, Username=testroleuser@test.com, Id=005Z0000000JlfsIAC, LocaleSidKey=en_US, ProfileId=00eG00000013lksIAA, LastName=Testing, Country=United States}
20:11:18.156 (5156696000)|SOQL_EXECUTE_BEGIN|[35]|Aggregations:0|select Id from User where Id = :tmpVar1
20:11:18.187 (5187712000)|SOQL_EXECUTE_END|[35]|Rows:1

JBabuJBabu

Hi,

 

Can you please tell me why I am not able to see the  (I have run the test class directly from front end instead of using eclipse)

*** user details  in my debug log.

This is leading to balnk value in line 36 (below debug statement)

 

system.debug('*** user details' +userdata);

which is leading to error message "attempt to de-reference a null object

 

Can you please let me know what is the issue from my side?

I think you might not get the error message.

 

Thanks,

JBabu.


thedabblerthedabbler

ok This is the code I am running.I got rid of other code as I don't need those.

 

Run this and see if that debug logs show up or not:

 

@isTest(seeAllData=true)
public class TestBiggerRevAccounts{

private static List<Account> testAccountsList;
static User sysAdminUser;
private static User userdata;
private static final Integer acct_cnt = 10;
static {
sysAdminUser = [select Id from User
where Profile.Name='System Administrator' and IsActive = true
limit 1];

System.runAs(sysAdminUser) {
userRole userroledata = new UserRole(Name = 'Test Executive');
insert userroledata;

Profile profiledata = [select Id FROM profile where name='System Administrator'];

userdata = new User(alias = 'user1', userroleid = userroledata.Id, email='testroleuser@test.com', emailencodingkey='UTF-8', lastname='Testing', languagelocalekey='en_US',
localesidkey='en_US', profileid = profiledata.Id, country='United States',
timezonesidkey='America/Los_Angeles', username='testroleuser@test.com');

insert userdata;
system.debug('*** user data' +userdata);
}
}

public static testMethod void BiggerRevAccountsUnitTest()
{
System.runAs(sysAdminUser) {


Test.StartTest();
system.debug('*** user details' +userdata);
User user = [select Id from User where Id = :userdata.Id];


Test.StopTest();


}


}

}

 

I can only suspect some other DML failing in your code. Or is there Debug log setting set to higher mode than debug like in ERROR or WARN.

 

JBabuJBabu

Hi,

 

Ya, when I ran the code given by you it worked. But when I use it with actual code I am not getting it.

 

Thanks,

JBabu.