I'm trying to unit test the class below, which does against the ProcessInstanceWorkitem table to find all pending approvals for an sobject (given by name in the method call). The class is rather generic, designed to work against any sobject and independently of any specifically defined Approval Process. Its supposed to be packaged as part of a Lightning Component.
I'm struggling with properly unit testing this class since I do not have or want to define an approval process in the Setup menu (since then I'd have to package that one as well).
Is there any way to properly test a class like this without having an approval process be defined in the Org?
I've tried inserting a ProcessInstanceWorkItem manually in a test class, but this doesn't work since I'd need to insert a ProcessDefinition first (where I get a DML not supported exception).
public class MyApprovalsCtrl {
@AuraEnabled
public static List<sobject> getPendingApprovals(String sobjectName, String[] fields) {
Id userId = UserInfo.getUserId();
List<ProcessInstanceWorkitem> wis = [
SELECT Id, ProcessInstance.TargetObjectId
FROM ProcessInstanceWorkitem
WHERE ProcessInstance.Status = 'Pending'
AND ProcessInstance.TargetObject.Type = :sobjectName
AND ActorId = :userId
];
if(wis.size() > 0) {
String idList = '';
for(ProcessInstanceWorkitem wi: wis) {
idList += '\'' + wi.ProcessInstance.TargetObjectId + '\'' + ',';
}
idList = idList.removeEnd(',');
String fieldList = '';
for(String field: fields) {
if(field != null && !String.isBlank(field))
fieldList += ',' + field;
}
String query = 'SELECT Id'+ fieldList + ' FROM ' + sobjectName + ' WHERE Id IN (' + idList + ')';
List<sobject> objectsWithPendingApproval = Database.query(query);
return objectsWithPendingApproval;
}
else {
return new List<sobject>();
}
}
}