+ Start a Discussion

Test Method Case with Queue Owner

Hey All,
Getting an error in a test method when trying to set a queue as a case owner. The code looks like:
Account testAcc = new Account(Name='TestAcc',AccountNumber='12345');
insert testAcc;
Contact testCont = new Contact(LastName='Testcontact',AccountId=testAcc.Id);
insert testCont;
Group testGroup = new Group(Name='TestingQueue',Type='Queue');
insert testGroup;
Case retCase = new Case(AccountId=testAcc.Id, ContactId=testCont.Id, Description='A sample description', Severity__c='Critical', Status='Assistance Required', Subject='Test subject',OwnerId=testGroup.Id);
insert retCase;

but this throws the error:
System.DmlException: Insert failed. First exception on row 0; first error: INVALID_OPERATION, Queue not associated with this SObject type

 Is there a reason this error is happening? When i hardcode an existing queue id as the ownerid I do not get this error!
Thanks in advance for any help at all
Vijay RautVijay Raut
Hi Dave,

Apex doesnot allow DML operation on setup objects (like Group, Profile, Role,....) inside testmethod. Hence your Group Insert in failing in testmethod.