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
dawnzdydawnzdy 

how to populate userinfo.username in testmethod?

Hi,

   I have a before insert trigger to execute when a case is logged from selfservice portal.
   It works fine but I have a problem of passing the test.
   
   In the trigger:
 
   ID contactId = [select contactId from SelfServiceUser where username=:UserInfo.getUserName()].contactId;

   Then in the testmethod how to prepopulate the value to UserInfo.getUserName(), so the test can pass?
aalbertaalbert
In your test method, create a new User so you can specify the username and all other required fields then use System.runAs().

There are more details on test methods and runAs specifically on this recent wiki article: http://wiki.apexdevnet.com/index.php/An_Introduction_to_Apex_Code_Test_Methods



dawnzdydawnzdy
It's very helpful and thanks so much for your quick response!

According to my test, the Userinfo can capture the information from both selfservice user and SF user.

The testmethod has to run as SF user.


Message Edited by dawnzdy on 01-08-2009 06:39 AM