Below is my apex class:
public void submitForApproval(List<Account> acc)
{
try{
for(SObject ac :acc) {
if((ac.Account_Record_Type_Name__c == 'A' || ac.Account_Record_Type_Name__c == 'B') && ac.Status__c == 'New'){
// Create an approval request for the Account
Approval.ProcessSubmitRequest submitRequest = new Approval.ProcessSubmitRequest();
submitRequest.setComments('Submitting request for approval');
submitRequest.setObjectId(ac.id);
submitRequest.setSubmitterId(ac.CreatedById);
submitRequest.setProcessDefinitionNameOrId('Account for Approval');
Approval.ProcessResult result = Approval.process(submitRequest);
system.debug('Result ' + result.isSuccess() + 'NewObj ID' + ac.Id);
}
}
}catch(Exception ex){
system.debug('Exception in Approval Process submision and error is--->'+ex);
Batch.trackErrorLog(CLASS_NAME, 'submitForApproval',
'Exception Line Number:' + ex.getLineNo() +
'Exception Type' + ex.getType()+
'Exception Message' + ex.getmessage());
}
}
I tried to write a test class like this:
Account testdelAcc=new Account(Name = 'Test DelAcc');
try{
testdelAcc.RecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByName().get('ABC').getRecordTypeId();
insert testdelAcc;
}
catch(Exception e)
{
system.assertEquals(e.getMessage(), e.getMessage());
}
I am trying to pass Invalid Record Type but it does not cover the catch block.
Can someone help me what I am missing here?
Best Answer
The way you have written test method logic, it will never enter in
catch
block as in your main class you are not throwing an exception when recordtype is Invalid.submitForApproval
method will throw the exception if system is unable to process the submit for approval.You can change your code as below if possible.
Pass
processDefinitionNameOrId
as an argument to this methodTest Class
From the test method, pass invalid process definition Id for which System will throw the exception.
One more point, your
assert
statement. What you are trying to compare with theassertEquals()
when you are putting both expected output in both the arguments.Follow best practices writing test class.
Without changing method signature, you could declare a variable for
processDefinitionNameOrId
Test class
Assign any invalid values from test class to
processDefinitionNameOrId
.