+ Start a Discussion
jjvdevjjvdev 

OwnerId.IsActive = false after assignment to test lead

The code below runs successfully with no errors.  The test leads ownerid and the activeuser Id are exactly the same.

So explain this to me???

15:44:36.155 (9155332602)|USER_DEBUG|[9]|DEBUG|***** user data *****User:{IsActive=true, Id=00517000000GYztAAG}

15:44:36.499 (9499882380)|USER_DEBUG|[15]|DEBUG|***** lead owner active *****false

How is the same user active in one query, but inactive in another???



 
@isTest
private class UserIsActiveTest {

    static testMethod void myUnitTest() {
        
	// get active users
	  List<User> reps = new List<User> ([select Id, IsActive from User where IsActive = true]);
	  User activeuser = reps.get(0);
	  system.debug ('***** user data *****'+reps.get(0));
	  
	  // insert test lead and assign to active user
	  Lead testLead = new Lead(LastName='Test1', Company='Test1 Inc.', Email='test1@test.com', Description='Description', OwnerId=activeuser.Id);
	  insert testLead;
	  system.assertEquals(activeuser.Id, testLead.OwnerId);
	  system.debug ('***** lead owner active *****'+testLead.Owner.IsActive);
	  
    }

}

 
ManojjenaManojjena
Hi jjvdev,

Hey you need to query from the test record to get the reference information .Also to use system.asset or assertEquals to test the record you need to query the record .Please try with below code it will work .
@isTest
private class UserIsActiveTest {

    static testMethod void myUnitTest() {
    // get active users
      List<User> reps = new List<User> ([select Id, IsActive from User where IsActive = true]);
      User activeuser = reps.get(0);
      system.debug ('***** user data *****'+reps.get(0));
      
      // insert test lead and assign to active user
      Lead testLead = new Lead(LastName='Test1', Company='Test1 Inc.', Email='test1@test.com', Description='Description', OwnerId=activeuser.Id);
      insert testLead;
      testLead=[SELECT id,Owner.IsActive FROM Lead WHERE id=:testLead.Id];
      system.assertEquals(activeuser.Id, testLead.OwnerId);
      system.debug ('***** lead owner active *****'+testLead.Owner.IsActive);
      
    }

}

Please check the salesforce link test class best practice Verify the results are correct section .

https://developer.salesforce.com/page/How_to_Write_Good_Unit_Tests  

Thnaks 
Manoj

 
jjvdevjjvdev
Added line 13 from your code but still receiving 
USER_DEBUG|[16]|DEBUG|***** lead owner active *****false

Updated version:
@isTest
private class UserIsActiveTest {

    static testMethod void myUnitTest() {
        
	// get active users
	  List<User> reps = new List<User> ([select Id, IsActive from User where IsActive = true]);
	  User activeuser = reps.get(0);
	  system.debug ('***** user data *****'+reps.get(0));
	  
	  // insert test lead and assign to active user
	  Lead testLead = new Lead(LastName='Test1', Company='Test1 Inc.', Email='test1@test.com', Description='Description', OwnerId=activeuser.Id);
	  insert testLead;
	  
	  testLead=[SELECT Id, OwnerId, Owner.IsActive FROM Lead WHERE id=:testLead.Id];
	  system.assertEquals(activeuser.Id, testLead.OwnerId);
	  system.debug ('***** lead owner active *****'+testLead.Owner.IsActive);
	  
    }
}

 
ManojjenaManojjena
Hi Jjvdev,

As the user we are getting from database that is why internally salesforce is making as false .

You can try with below code where I have created user in test context so it is showing true .
 
@isTest
private class UserIsActiveTest {
   static testMethod void myUnitTest() {
		Profile p = [SELECT Id FROM Profile WHERE Name='System Administrator']; 
        User activeuser = new User(Alias = 'sys', Email='systemadmin@testorg.com', 
                          EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US', 
                          LocaleSidKey='en_US', ProfileId = p.Id, 
                          TimeZoneSidKey='America/Los_Angeles', UserName='sysadmintest@ss.com');
        System.runAs(activeuser) { 
		// insert test lead and assign to active user
		  Lead testLead = new Lead(LastName='Test1', Company='Test1 Inc.', Email='test1@test.com', Description='Description', OwnerId=activeuser.Id);
		  insert testLead;
		  testLead=[SELECT id,Owner.IsActive FROM Lead WHERE id=:testLead.Id];
		  system.assertEquals(activeuser.Id, testLead.OwnerId);
		  system.debug ('***** lead owner active *****'+testLead.Owner.IsActive);
      }
   }
}

Please let me know any issue .