这是一个Apex测试类代码,我在这里得到的是,这个类将包含硬编码值来创建并提交票证以供审批。
我想知道我是否能在这里得到任何指导,因为我真的不能从其他地方得到。感谢您提供的任何帮助。
PS。这是Salesforce平台-Remedyforce相关。
@isTest
公共类Test_CustomRequireRecjectionComment{
/*
create an object for approval, then
simulate rejecting the approval with an added comment.
The rejection should be processed normally without being interrupted.
*/
private static testmethod void testRejectionWithComment()
{
// Generate sample work item using utility method.
Id testWorkItemId = generateAndSubmitObject();
// Reject the submitted request, providing a comment.
Approval.ProcessWorkitemRequest testRej = new Approval.ProcessWorkitemRequest();
testRej.setComments('Rejecting request with a comment.');
testRej.setAction ('Reject');
testRej.setWorkitemId(testWorkItemId);
Test.startTest();
// Process the rejection
Approval.ProcessResult testRejResult = Approval.process(testRej);
Test.stopTest();
// Verify the rejection results
System.assert(testRejResult.isSuccess(), 'Rejections that include comments should be permitted');
System.assertEquals('Rejected', testRejResult.getInstanceStatus(),
'Rejections that include comments should be successful and instance status should be Rejected');
}
/*
For this test, create an object for approval, then reject the request, then
without a comment explaining why. The rejection should be halted, and
and an apex page message should be provided to the user.
*/
private static testmethod void testRejectionWithoutComment()
{
// Generate sample work item using utility method.
Id testWorkItemId = generateAndSubmitObject();
// Reject the submitted request, without providing a comment.
Approval.ProcessWorkitemRequest testRej = new Approval.ProcessWorkitemRequest();
testRej.setComments('');
testRej.setAction ('Reject');
testRej.setWorkitemId(testWorkItemId);
Test.startTest();
// Attempt to process the rejection
try
{
Approval.ProcessResult testRejResult = Approval.process(testRej);
system.assert(false, 'A rejection with no comment should cause an exception');
}
catch(DMLException e)
{
system.assertEquals('Operation Cancelled: Please provide a rejection reason!',
e.getDmlMessage(0),
'error message should be Operation Cancelled: Please provide a rejection reason!');
}
Test.stopTest();
}
/*
When an approval is approved instead of rejected, a comment is not required,
mark the approval status as pending, then ensure that this functionality still holds together.
*/
private static testmethod void testApprovalWithoutComment()
{
// Generate sample work item using utility method.
Id testWorkItemId = generateAndSubmitObject();
// approve the submitted request, without providing a comment.
Approval.ProcessWorkitemRequest testApp = new Approval.ProcessWorkitemRequest();
testApp.setComments ('');
testApp.setAction ('Approve');
testApp.setWorkitemId(testWorkItemId);
Test.startTest();
// Process the approval
Approval.ProcessResult testAppResult = Approval.process(testApp);
Test.stopTest();
// Verify the approval results
System.assert(testAppResult.isSuccess(),
'Approvals that do not include comments should still be permitted');
System.assertEquals('Approved', testAppResult.getInstanceStatus(),
'All approvals should be successful and result in an instance status of Approved');
}
/*
Put many objects through the approval process, some rejected, some approved,
some with comments, some without. Only rejctions without comments should be
prevented from being saved.
*/
private static testmethod void testBatchRejctions()
{
List<BMCServiceDesk__Change_Request__c> testBatchIS = new List<BMCServiceDesk__Change_Request__c>{};
for (Integer i = 0; i < 200; i++)
{
testBatchIS.add(new BMCServiceDesk__Change_Request__c());
}
insert testBatchIS;
List<Approval.ProcessSubmitRequest> testReqs =
new List<Approval.ProcessSubmitRequest>{};
for(BMCServiceDesk__Change_Request__c testinv : testBatchIS)
{
Approval.ProcessSubmitRequest testReq = new Approval.ProcessSubmitRequest();
testReq.setObjectId(testinv.Id);
testReqs.add(testReq);
}
List<Approval.ProcessResult> reqResults = Approval.process(testReqs);
for (Approval.ProcessResult reqResult : reqResults)
{
System.assert(reqResult.isSuccess(),
'Unable to submit new batch invoice statement record for approval');
}
List<Approval.ProcessWorkitemRequest> testAppRejs
= new List<Approval.ProcessWorkitemRequest>{};
for (Integer i = 0; i < 50 ; i++)
{
Approval.ProcessWorkitemRequest testRejWithComment = new Approval.ProcessWorkitemRequest();
testRejWithComment.setComments ('Rejecting request with a comment.');
testRejWithComment.setAction ('Reject');
testRejWithComment.setWorkitemId(reqResults[i*4].getNewWorkitemIds()[0]);
testAppRejs.add(testRejWithComment);
Approval.ProcessWorkitemRequest testRejWithoutComment = new Approval.ProcessWorkitemRequest();
testRejWithoutComment.setAction ('Reject');
testRejWithoutComment.setWorkitemId(reqResults[(i*4)+1].getNewWorkitemIds()[0]);
testAppRejs.add(testRejWithoutComment);
Approval.ProcessWorkitemRequest testAppWithComment = new Approval.ProcessWorkitemRequest();
testAppWithComment.setComments ('Approving request with a comment.');
testAppWithComment.setAction ('Approve');
testAppWithComment.setWorkitemId(reqResults[(i*4)+2].getNewWorkitemIds()[0]);
testAppRejs.add(testAppWithComment);
Approval.ProcessWorkitemRequest testAppWithoutComment = new Approval.ProcessWorkitemRequest();
testAppWithoutComment.setAction ('Approve');
testAppWithoutComment.setWorkitemId(reqResults[(i*4)+3].getNewWorkitemIds()[0]);
testAppRejs.add(testAppWithoutComment);
}
Test.startTest();
// Process the approvals and rejections
try
{
List<Approval.ProcessResult> testAppRejResults = Approval.process(testAppRejs);
system.assert(false, 'Any rejections without comments should cause an exception');
}
catch(DMLException e)
{
system.assertEquals(50, e.getNumDml());
for(Integer i = 0; i < 50 ; i++)
{
system.assertEquals((i*4) + 1, e.getDmlIndex(i));
system.assertEquals('Operation Cancelled: Please provide a rejection reason!',
e.getDmlMessage(i));
}
}
Test.stopTest();
}
/*
Utility method for creating single object, and submitting for approval.
The method should return the Id of the work item generated as a result of the submission.
***Include required field and set status.
*/
private static Id generateAndSubmitObject()
{
// Create a sample object and then submit it for approval.
BMCServiceDesk__Change_Request__c testIS = new BMCServiceDesk__Change_Request__c();
testIS = [Select Id From BMCServiceDesk__Change_Request__c Where Name ='CR00002135'];
insert testIS;
Approval.ProcessSubmitRequest testReq = new Approval.ProcessSubmitRequest();
testReq.setObjectId(testIS.Id);
Approval.ProcessResult reqResult = Approval.process(testReq);
System.assert(reqResult.isSuccess(),'Unable to submit new invoice statement record for approval');
return reqResult.getNewWorkitemIds()[0];
}
}
在测试运行测试类后,我收到了以下错误消息,
~~~~~~
[查看]0:00测试_自定义需求弹出注释testApprovalWithoutComment失败System.QueryException:列表没有可分配给SObject的行Class.Test_CustomRequireRecjectionComment.generateAndSubmitObject:第187行,第1列Class.Test_CustomRequireRecjectionComment.testApprovalWithoutComment:第71行,第1列
[查看]0:18测试_自定义需求弹出注释testBatchRejections失败System.DmlException:进程失败。第0行的第一个异常;第一个错误:NO_APPLICABLE_PROCESS,未找到适用的审批流程。:[]Class.Test_CustomRequireRejectionComment.TestBatchRejections:第115行,第1列
[查看]0:00测试_自定义需求弹出注释testRejectionWithComment失败System.QueryException:列表没有可分配给SObject的行Class.Test_CustomRequireRecjectionComment.generateAndSubmitObject:第187行,第1列Class.Test_CustomRequireRejectionComment.testRejectionWithComment:第13行,第1列
[查看]0:00测试_自定义需求弹出注释testRejectionWithoutComment失败System.QueryException:列表没有可分配给SObject的行Class.Test_CustomRequireRecjectionComment.generateAndSubmitObject:第187行,第1列Class.Test_CustomRequireRecjectionComment.testRejectionWithoutComment:第40行,第1列
问题似乎出现在generateAndSubmitObject()静态方法中。有很多奇怪的行为。
我要把它一行一行地分解。
第1行:testIS获取一个新实例化的BMCServiceDesk__Change_Request__c对象。
第2行:testIS获取名称等于"CR00002135"的BMCServiceDesk__Change_Request__c对象的列表(但只有ID字段)。这里有两个问题。首先,这个动作使1号线完全无用。第二,这不会返回任何结果。您没有任何名为CR00002135的CR,这是您目前的数据库。
第3行:您正在将此空列表重新插入数据库。这又是一次无操作。
如果修复了第1-3行,那么剩下的部分应该可以工作。我的建议是,去掉第2行并更新第1行,这样当您实例化BMCServiceDesk__Change_Request__c对象时,它就可以获得您想要的所有信息。