+ Start a Discussion
SS KarthickSS Karthick 

Regarding Test code

Hi folks,
     Can anyone tell me what is the problem in my test class??

Apex Trigger:
  trigger Test_Share on Test__c (after insert) {
   
    Id UserId=UserInfo.getUserId();
    Boolean bln=false;
    List<Id> ParentUserId = new List<Id>();
    User u = new User();
    while(bln == false){
       
      u = [select id,ManagerId from user where id = :UserId];
       if(u.ManagerId == null){
           
          bln = true;
      }
       else{
          ParentUserId.add(u.ManagerId);
          UserId = u.ManagerId;
      }
  
  }
  
    //ID groupId = [select id from Group where Type = 'Organization'].id;
    Id parent;
        if(trigger.isInsert){
   
    List<Test__Share> testShare= new List<Test__Share>();
         for(Test__c t : trigger.new){
            
           parent=t.Id;
          
           for(Integer j=0;j<ParentUserId.size();j++)
           {
               
                //string ob='ts'+coun;
                Test__Share ob= new Test__Share();
                ob.ParentId = parent;
                ob.UserOrGroupId =ParentUserId[j];
               
                system.debug('Id='+ParentUserId[j]);


       
                ob.AccessLevel = 'edit';

                ob.RowCause = Schema.Test__Share.RowCause.Test_Access__c;
                testShare.add(ob);
                //coun++;
                              
           }
        }
       
            if(!testShare.isEmpty())
                insert testShare;
           
       
            // Database.SaveResult[] TestShareInsertResult = Database.insert(testShare,false);
    }
}

    My Test Class:
            @isTest
private class TestShareTrigger {
    static testMethod void testShares() {
       
        List<Test__c> deleteTestData= [select id from Test__c limit 50000];
        if(deleteTestData.size()>0)
            delete deleteTestData;
       
        Set<ID> ids = new Set<ID>();
        List<User> users = new List<User>();
        List<User> updatedusers = new List<User>();
        String pid = [Select id from Profile where Name='Test Reps'].Id;
        for(integer i=1; i<=200; i++){
     
  
      User tuser = new User(  firstname = 'test',
                            lastName = 'Name'+i,
                            email = 'testname'+i+'@email.com',
                            Username = 'testname'+i+'@email.com',
                            EmailEncodingKey = 'ISO-8859-1',
                            Alias = 'test',
                            TimeZoneSidKey = 'America/Los_Angeles',
                            LocaleSidKey = 'en_US',
                            LanguageLocaleKey = 'en_US',
                            ProfileId = pid);
      users.add(tuser);
                           
  }
        insert users;
       
       
      
       
        for(Integer i=0;i<users.size();i++){
                             
            if(i!=199){
                users[i].ManagerId=users[i+1].Id;
                 
            }
           
        }
       update users;
       
   
        //List<User> u = [SELECT Id FROM User where LastName LIKE 'testuse%'];
        Test__c t=new Test__c();
        t.Email__c='test@gmail.com';
        t.Mobile__c='12345678';
        t.OwnerId=users[0].id;
       
        insert t;
       
       
       
        List<Test__Share> testsha = [select id from Test__Share where AccessLevel = 'edit'];
        System.debug('TestShares='+testsha.size());
        System.assert( testsha.size()>1);
       
       
      
       
    }

}

My test case scenario is:
If user1 creates record then it is visible to all the user(ie visible to all 200 user )
and If user200 creates records then it is visible to himself ..

For that how to write the test code??

Thanks in advance
Karthick
Best Answer chosen by SS Karthick
Ramu_SFDCRamu_SFDC
You need to use runas() method to test if the 200th record is not visible to other users. Below it he article on how to use runas() method

https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_testing_tools_runas.htm

All Answers

Ramu_SFDCRamu_SFDC
You need to use runas() method to test if the 200th record is not visible to other users. Below it he article on how to use runas() method

https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_testing_tools_runas.htm
This was selected as the best answer
Gaurav NirwalGaurav Nirwal
you can extend the limits of record then this type of problem can be occured in salesforce