+ Start a Discussion

how to populate userinfo.username in testmethod?


   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?
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

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