[SalesForce] Can’t Insert User in Test Class

I've created a Trigger on the User object and I'm trying to test creation, update, deletion and undeletion of a User.

Starting with Insert DML, I've got the following code:

Profile p = [SELECT Id FROM Profile WHERE Name = 'Recruitment Consultant'];

User u = new User(
  Alias = 'testu',
  Email = 'test@example.com',
  EmailEncodingKey = 'UTF-8',
  LastName = 'Test',
  LanguageLocaleKey = 'en_US',
  LocaleSidKey = 'en_US',
  TimeZoneSidKey='America/Los_Angeles',
  ProfileId = p.Id,
  UserName='test@example.com'
);

INSERT u;

Which should, in theory, check check the Trigger's Insert methods.

trigger UserTrigger on User (before insert, before update, before delete, after insert, after update, after delete, after undelete) {
  if (Trigger.isBefore) {
    if (Trigger.isInsert) {

    }
    // More stuff happens after this...

For some reason though, I'm getting the following error:

(UserTriggerTest) Invalid constructor syntax, name=value pairs can only be used for SObjects

I tried doing it in the following format instead:

User u = new User();

u.Alias = 'testu';
[...]

INSERT u;

But then I'm getting an error that the variable Alias doesn't exist.

I'm confused here because I can't see a problem with what I'm doing! Any help would be appreciated.

Thanks.

Edit
I'll include the full class and test class here, it's literally a skeleton for future triggers, but you'll get the idea:

Trigger

trigger UserTrigger on User (before insert, before update, before delete, after insert, after update, after delete, after undelete) {
  if (Trigger.isBefore) {
    if (Trigger.isInsert) {

    }
    else if (Trigger.isUpdate) {

    }
    else if (Trigger.isDelete) {

    }
  }
  else {
    if (Trigger.isInsert) {

    }
    else if (Trigger.isUpdate) {

    }
    else if (Trigger.isDelete) {

    }
    else if (Trigger.isUndelete) {

    }
  }
}

Test Class

@isTest
private class UserTriggerTest {
  @isTest static void testInsert() {
    Profile p = [SELECT Id FROM Profile WHERE Name = 'Recruitment Consultant'];

    User u = new User(
      Alias = 'testu',
      Email = 'test@unitingambition.com',
      EmailEncodingKey = 'UTF-8',
      LastName = 'Test',
      LanguageLocaleKey = 'en_US',
      LocaleSidKey = 'en_US',
      TimeZoneSidKey='America/Los_Angeles',
      ProfileId = p.Id,
      UserName='test@unitingambition.com'
    );

    INSERT u;
  }

  @isTest static void testUpdate() {

  }

  @isTest static void testDelete() {

  }

  @isTest static void testUndelete() {

  }
}

Best Answer

This seems to be most likely caused by a naming conflict. If you created an Apex Class as follows, you would no longer be able to construct User records as you are trying to do:

public class User
{
    public User()
    {
        // default constructor takes no parameters
        // there is no way to pass in name=value pairs via Apex
    }
}