1

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);
}
2

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.