Friday, July 6, 2012

Using the runAs Method in Salesforce

Topic Cover By this Post:--
= How to write Test Class for Triggers
= How to use runAs Method in Test Class
= How to use User in Test Class

Generally, all Apex code runs in system mode, and the permissions and record sharing of the current user are not taken into account. The system method runAs enables you to write test methods that change either the user contexts to an existing user or a new user, or to run using the code from a specific version of a managed package. When running as a user, all of that user's record sharing is then enforced. You can only use runAs in a test method. The original system context is started again after all runAs test methods complete.
  
This Examples also illustrate how to write Test Class for a Trigger in Salesforce.

Trigger:---

trigger updateMultiPickList on Account (before insert){
 
  //Fetching Current User Details
   User u = [Select Department From User Where id = :UserInfo.getUserId()];

    //Loop through New Accounts
     for(Account acc : Trigger.New){
         if(u.Department=='STP'){
             acc.stability__c = 'STP';
         }
         if(u.Department =='SHC'){
             acc.stability__c = 'SHC';
         }
     }
}


Test Class:----



@isTest
private class TestupdateMuliPicklist{
   
   //Testing When User Department field have Value STP
    static testMethod void myUnitTest() {
      // Setup test data 
      // This code runs as the system user 
    
      //Profile    
       Profile p = [SELECT Id FROM Profile WHERE Name='Standard User']; 
      
      //User
      User u = new User(Alias = 'standt', Email='standarduser@testorg.com', 
                        EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US', 
                        LocaleSidKey='en_US', ProfileId = p.Id, department = 'STP',
                        TimeZoneSidKey='America/Los_Angeles', UserName=Math.random() + '@test.com');

      System.runAs(u) {
      // The following code runs as user 'u'  
    
      //Asserting Values
      System.debug('Current User: ' + UserInfo.getUserName());
      System.debug('Current Profile: ' + UserInfo.getProfileId()); 
      
      //Account
       Account account = new Account(Name = 'test Account',OwnerId = u.Id, Stability__c = 'ABC');
     
         //Start Test Here
          Test.startTest();
     
         //Insert Account
          insert account;
     
         //Stop Test Here
          Test.stopTest();
    }
}

//Testing When User Department field have Value SHC
    static testMethod void myUnitTest1() {
      // Setup test data 
      // This code runs as the system user 
    
      //Profile    
       Profile p = [SELECT Id FROM Profile WHERE Name='Standard User']; 
      
      //User
      User u = new User(Alias = 'standt', Email='standarduser@testorg.com', 
                        EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US', 
                        LocaleSidKey='en_US', ProfileId = p.Id, department = 'SHC',
                        TimeZoneSidKey='America/Los_Angeles', UserName=Math.random() + '@test.com');

      System.runAs(u) {
      // The following code runs as user 'u'  
    
      //Asserting Values
      System.debug('Current User: ' + UserInfo.getUserName());
      System.debug('Current Profile: ' + UserInfo.getProfileId()); 
      
      //Account
       Account account = new Account(Name = 'test Account',OwnerId = u.Id, Stability__c = 'ABC');
     
         //Start Test Here
          Test.startTest();
     
         //Insert Account
          insert account;
     
         //Stop Test Here
          Test.stopTest();
    }
}
}
 
Finally you got a coverage of 100% for Trigger as:--
 
 














No comments:

Post a Comment