I have a generic piece of code that is responsible of approve a record. It does not assume the SObject type nor the process definition.
For example, something like the following:
public static Boolean approveRecord(Id recordId, String comment, User nextUser) {
ProcessInstanceWorkitem workItemId = [
SELECT Id
FROM ProcessInstanceWorkitem
WHERE ProcessInstance.TargetObjectId = :recordId
];
Approval.ProcessWorkitemRequest appRequest = new Approval.ProcessWorkitemRequest();
appRequest.setComments(comment);
appRequest.setAction('Approve');
appRequest.setNextApproverIds(new List<Id>{nextUser});
appRequest.setWorkitemId(workItemId.Id);
Approval.ProcessResult result = Approval.process(appRequest);
return result.isSuccess();
}
I want to test this piece of code without losing generability. It was already asked on salesforce.SE how to create an approval process definition programmatically within test classes without success. My thought is that maybe is possible to mock the behavior.