テストクラスはそのまま実行とやると、ログインしているユーザーのプロファイル?で実行されてしまう。

が、しかし、標準ユーザのプロファイルで実行したいときは?ってことで、調べてみると書き方を見つけたのでメモりました。

// 標準ユーザのプロファイル
Profile prof = [ SELECT Id, UserType, Name FROM Profile WHERE Name = :'標準ユーザ' LIMIT 1 ];

// 取引先
Account a = new Account( Name = 'An Account' );
insert a;

// 取引先責任者
Contact c = new Contact( LastName = 'A Contact', AccountId = a.Id );
insert c;

// 標準ユーザのプロファイルのユーザー
User testUser = new User(
            Alias = 'newUser',
            Email = 'newuser@testorg.com',
            EmailEncodingKey = 'UTF-8',
            LastName = 'Testing',
            LanguageLocaleKey = 'en_US',
            LocaleSidKey = 'en_US',
            ProfileId = prof.Id,
            TimeZoneSidKey = 'America/Los_Angeles',
            UserName = 'newuser@testorg.com',
            ContactId = c.Id
);

// 標準ユーザのプロファイルのユーザーでテストを実行
System.runAs( testUser ) {


            テストのコードをガリゴリ・・・


}

独特ですね。。。