I am trying to write a test class for my Apex trigger, and I am setting up a user in the following way:

User u1 = [SELECT Id FROM User WHERE profileId IN (SELECT id FROM Profile WHERE Name = 'System Administrator') AND isActive = TRUE LIMIT 1];

    { ...}

When I run my test, I always get the following error message:

System.TypeException: System.runAs can only be used with an active user

I don't understand as my user is set to active...

Tia Reena

  • 1
    This doesn't make sense, are you sure your changes were saved (assuming you are using an IDE). Sometimes the IDE (Eclipse or Mavensmate) report that the change but it doesn't actually save the new code. Have you verified by viewing the test from Salesforce?
    – PepeFloyd
    Jun 9, 2014 at 12:04
  • yes, it was saved. i always thought that in apex things were case insensitive, but I tried it out anyway, and changed TRUE to true and now it works...
    – user8737
    Jun 9, 2014 at 12:23

2 Answers 2


Rather than have dependencies on your current user records (which cause problems like this) in your tests, it is usually beneficial to actually create test users as part of your test, during the initialisation. Like this....

static testmethod void myProfileSpecificTest()
    // Let's create a new test user
    Profile testProfile = [SELECT Id 
                           FROM profile
                           WHERE Name = 'System Administrator' 
                           LIMIT 1];

    User testUser = new User(LastName = 'test user 1', 
                             Username = '[email protected]', 
                             Email = '[email protected]', 
                             Alias = 'testu1', 
                             TimeZoneSidKey = 'GMT', 
                             LocaleSidKey = 'en_GB', 
                             EmailEncodingKey = 'ISO-8859-1', 
                             ProfileId = testProfile.Id, 
                             LanguageLocaleKey = 'en_US');     

            .....Do Some Testing...

    ..... assert the results are correct .....

You don't even have to insert the new user in the test, and any new users you create as part of your test are always guaranteed to be active, plus are free from any licence restrictions.

Further details and examples can be found at: http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_testing_tools_runas.htm


It seems that I am now able to answer the question myself. The solution was as simple as changing the code to:

User u1 = [SELECT Id FROM User WHERE profileId IN (SELECT id FROM Profile WHERE Name = 'System Administrator') AND isActive = true LIMIT 1];
  • What change was made? That looks like the same code you have in your question.
    – BarCotter
    Jun 9, 2014 at 12:23
  • I see the change now. TRUE was changed to true
    – BarCotter
    Jun 9, 2014 at 12:24
  • 7
    I have a hard time believing this is the solution, since I use TRUE and true interchangeably. So now, if you change TRUE back to true, is it broken again? Jun 9, 2014 at 13:30

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .