function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Simon234Simon234 

How to test a change OwnerId by groupQueue.Id?

I have a trigger. If a user who creates a record isn't 'Manager' (UserRole.Name) - record's OwnerId change to GroupQueue.Id. I have a correct test method for scenario, where user-creator is 'Manager', but I don't know how to test if he isn't. I get an error "System.AssertException: Assertion Failed: Expected: 0,Actual 1" here. My code works right. I just can't make this test method. How can I fix it?

Helper:
public static void beforeInsert(List<Obj__c> : newList) {   //for before insert trigger

        Map<Id, User> userMap = new Map<Id, User>([SELECT Id FROM User WHERE UserRole.Name = 'Manager']);        
        Group groupQueue = [SELECT Id FROM Group WHERE Type = 'Queue' AND Name = 'Object Queue' LIMIT 1];
		
 		for(Obj__c obj: newList){
		 		if(!userMap.containsKey(obj.OwnerId)){
					obj.OwnerId = groupQueue.Id;
		 		} 
		}
	}
Test:
@isTest
    private static void createObjByNotManager(){
        
        UserRole userRole = new UserRole(Name = 'Somebody else');
        insert userRole;
        
        Profile profileId = [SELECT Id FROM Profile LIMIT 1];

        User user = new User(
            UserRoleId = userRole.Id,
            LastName = 'Test Code',
			Email = 'test@test.com',
			Alias = 'Tcode',
			Username = 'test1234444@testzsdfsdtfsdf.com',
			CommunityNickname = 'test12',
			LocaleSidKey = 'en_US',
			TimeZoneSidKey = 'GMT',
			ProfileID = profileId.Id,
			LanguageLocaleKey = 'en_US',
			EmailEncodingKey = 'UTF-8'
        );
        insert user;
        
		Group testGroup = new Group(Name = 'Object Queue', Type = 'Queue');
        insert testGroup;
        insert new QueueSobject(QueueId = testGroup.Id, SObjectType = 'Obj__c');
        
        Obj__c obj = new Obj__c(
            Name = 'Test Object'
        );
        System.runAs(user) {
            insert obj;
		}
        
        List<Obj__c> objList = [SELECT OwnerId FROM Obj__c WHERE OwnerId =: testGroup.Id];
        System.assertEquals(positionList.size(), 1);
    }
NagendraNagendra (Salesforce Developers) 
Hi Simon,

You don't need to create a Queue because it should already exist. Your trigger queries a random queue named 'Object Queue', which most likely won't be the one you created previously (the database tends to sort by Id when no other sort order is in the query). In other words, you just need to query for the existing group:
Group testGroup = [SELECT Id FROM Group WHERE Type = 'Queue' AND Name = 'Object Queue'];
If you want your unit test to work regardless of the queue, you could delete the old queue first (it's only for the unit test). However, I would leave it just like this, because if the query fails, it will indicate a problem in your trigger, which presumes that the queue exists. I also avoid limiting it to 1, because if there's more than one such queue, we want the test to fail, because otherwise your records might be assigned to the wrong queue randomly.

Kindly mark this as solved if it's resolved.

Thanks,
Nagendra
v varaprasadv varaprasad
Hi Simon,

Try This : 
 
@isTest
    private static void createObjByNotManager(){
        
        UserRole userRole = new UserRole(Name = 'Somebody else');
        insert userRole;
        
        Profile profileId = [SELECT Id FROM Profile LIMIT 1];

        User user = new User(
            UserRoleId = userRole.Id,
            LastName = 'Test Code',
			Email = 'test@test.com',
			Alias = 'Tcode',
			Username = 'test1234444@testzsdfsdtfsdf.com',
			CommunityNickname = 'test12',
			LocaleSidKey = 'en_US',
			TimeZoneSidKey = 'GMT',
			ProfileID = profileId.Id,
			LanguageLocaleKey = 'en_US',
			EmailEncodingKey = 'UTF-8'
        );
        insert user;
        
		 System.runAs(user) {
        
          Obj__c obj = new Obj__c(
            Name = 'Test Object'
            );
       
            insert obj;
          
		  List<Obj__c> objList = [SELECT OwnerId FROM Obj__c WHERE OwnerId =: testGroup.Id];

           System.assertEquals(objList.size(), 1);
		
		}
        
      
    }

Hope this helps you!
If my answer helps resolve your query, please mark it as the 'Best Answer' & upvote it to benefit others.



Thanks
Varaprasad
@For SFDC Support: varaprasad4sfdc@gmail.com
Blog: http://salesforceprasad.blogspot.com/

Salesforce latest interview questions  :
https://www.youtube.com/channel/UCOcam_Hb4KjeBdYJlJWV_ZA?sub_confirmation=1
Raj VakatiRaj Vakati
Try this
 
@isTest
    private static void createObjByNotManager(){
        
        UserRole userRole = new UserRole(Name = 'Somebody else');
        insert userRole;
        
        Profile profileId = [SELECT Id FROM Profile LIMIT 1];

        User user = new User(
            UserRoleId = userRole.Id,
            LastName = 'Test Code',
			Email = 'test@test.com',
			Alias = 'Tcode',
			Username = 'test1234444@testzsdfsdtfsdf.com',
			CommunityNickname = 'test12',
			LocaleSidKey = 'en_US',
			TimeZoneSidKey = 'GMT',
			ProfileID = profileId.Id,
			LanguageLocaleKey = 'en_US',
			EmailEncodingKey = 'UTF-8'
        );
        insert user;
        
		Group testGroup = new Group(Name = 'Object Queue', Type = 'Queue');
        insert testGroup;
        QueueSobject so=  new QueueSobject(QueueId = testGroup.Id, SObjectType = 'Obj__c');
        insert so ;
        Obj__c obj = new Obj__c(
            Name = 'Test Object'
        );
        System.runAs(user) {
            insert obj;
		}
		
		obj.OwnerId =so.Id ;
		
		update obj;
        
        List<Obj__c> objList = [SELECT OwnerId FROM Obj__c WHERE OwnerId =: testGroup.Id];
        System.assertEquals(positionList.size(), 1);
    }