+ Start a Discussion
Chris760Chris760 

Simulate a user logging in on a test class?

I have a trigger that utilizes the Last Login field on the user record (you need to have logged in within a certain period of time to start getting a case load assigned to you), but I can't seem to figure out how to simulate a test user logging into salesforce on a test class.  I've tried inserting a UserLogin record manually to create the needed "Last Login" record, but no dice... the system wouldn't let me.  Is there some other way that I could simulate the act of a test user logging into salesforce after I've created that test user?

Any ideas would be welcome.
Shashikant SharmaShashikant Sharma
try 

System.runAs( u ) {

}

where u is the user instance.
Chris760Chris760
Thanks Shashikant, but it didn't work.  The runAs seems to only have enforced sharing rules, but didn't actually create any login records.  Any other ideas?
MaheshwarMaheshwar
Hi Chris,

Were you able to create the login records?

I am in the same situation.
I need to login as the test user in test class in order to create a record on LoginHistory.


Thanks,
Mahesh