I have created a trigger which is dependant on a custom record type existing. In my test for the trigger, I want to ensure that when this record type doesn't exist the trigger throws the appropriate exception. Unfortunately I can't see how I can do this, as DML statements can't be performed on Record Types. The error message I am getting is: "DML not allowed on RecordType" see the trigger code below:
trigger SetMostRecent on Opportunity (before insert) {
List<RecordType> recordType = [SELECT id FROM RecordType WHERE Name = 'foo' AND SobjectType = 'Opportunity' limit 1];
Id recordTypeId;
if (recordType.size() > 0) {
recordTypeId = recordType[0].Id;
}
else {
throw new MembershipTypeNotFoundException('blahblah');
}
My test class is as follows:
static testMethod void myTest() {
//Creates a valid opportunity.
Opportunity opp = Data.ValidOpp();
RecordType type = [SELECT id, Name FROM RecordType WHERE Name = 'foo' AND SobjectType = 'Opportunity'];
//Line which throws DML not allowed on RecordType
delete type;
//should now throw custom exception..
insert opp;
}
Does anyone know a way how I could get around this?
Cheers