System.runAs():
This post resembles how you can use runas method of system class for testing your code by profile wise or user wise.
public class TestRunAs2 {
public static testMethod void test2() {
Profile p = [SELECT Id FROM profile WHERE name='Standard User'];
User u2 = new User(alias = 'newUser', email='newuser@testorg.com',
emailencodingkey='UTF-8', lastname='Testing',
languagelocalekey='en_US', localesidkey='en_US', profileid = p.Id,
timezonesidkey='America/Los_Angeles', username='newuser@testorg.com');
System.runAs(u2) {
// The following code runs as user u2.
// all your test data, created here in
// this block will not run in system mode.
// So access to org level data
// will be pretty limited
// Create all your Test Data + Structures till here
// use startTest() to give some extra buffer
// to your real test code
Test.startTest();
// Do any extra DML and other stuff
// you have more bandwidth now.
Test.stopTest();
}
// Any other validation and DML statements
// if required to validate the results.
}
}
No comments:
Post a Comment