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
Adnan PAdnan P 

Apex Unit Test - Clone Case with Inactive Record Type

Hello,

I'm hoping to get a little help with a unit test for the code below. Basicially, we have a custom clone button and I'm trying to check whether a user is trying to clone a Case with an inactive Record Type. If so, I'd like to display a warning message that let's the user know that a Case with an inactive Record Type cannot be cloned. The cloneCase() method from my controller is shown below.
 
public PageReference cloneCase() {
    	List<String> fieldsList = new List<String> {'RecordType.isActive'};
        Case parentCase = (Case)Database.query(SelectStarService.createSelectStarSoql('Case', fieldsList) + ' WHERE Id = :parentCaseId');
        if (parentCase.RecordType.isActive) {
	        clonedCase = parentCase.clone(false, true, false, false);
	        clonedCase = validatePicklistValues();
	        clonedCase.OwnerId = UserInfo.getUserId();
	        clonedCase.Cloned__c = true;
	        clonedCase.Origin = 'Cloned';
	        clonedCase.Incident_Date__c = System.now();
	        insert clonedCase;
	        PageReference pr = new PageReference('/' + clonedCase.Id + '/e?retURL=%2F' + clonedCase.Id);
	        return pr;
        } else {
        	ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.WARNING,
        	'You cannot clone a Case with an inactive Record Type.');
        	ApexPages.addMessage(msg);
        	return null;
        }
    }

I've tested and confirmed that the code above works in my sandbox. The issue I'm having is writing the unit test to trigger the else statement above. I can't figure out how to update the Record Type in my unit test so that the testCase has an inactive Record Type when a new Case is cloned from the testCase. I assumed I could insert testCase with an active Record Type and the perform an update to assign an inactive Record Type to testCase but it doesn't look like the system allows me to do that.

I get a "Attempt to de-reference a null object" when running the test. Obviously, this makes sense but I'm not sure how to fix it. I'm fairly new to coding so I'm not sure if there's something obvious that I'm mising. Any feedback would be greatly appreciated.
 
@isTest
	public static void testCloneCaseInactiveRT(){
		Test.startTest();
		Case testCase = (Case)SmartFactory.createsObject('Case');
		RecordType rt = [SELECT Id FROM RecordType WHERE SObjectType = 'Case' AND IsActive = false LIMIT 1];
		testCase.RecordType.Id = rt.Id;
		insert testCase;
		ApexPages.StandardController sc = new ApexPages.StandardController(testCase);
		CaseCloneController controller = new CaseCloneController(sc);
		PageReference pr = controller.cloneCase();
		System.assertEquals(null, controller.clonedCase.Id);
		Test.stopTest();
	}
Thank you!