这是一个Apex测试类代码,这里我拥有的是这个类将包含硬代码值来创建提交票据以供批准。
我想知道我能不能在这里得到任何指导,因为我真的不能从其他地方得到指导。谢谢您的帮助。
PS。这是Salesforce平台- Remedyforce相关的。
@isTest公共类Test_CustomRequireRejectionComment {
/*
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 Class.Test_CustomRequireRejectionComment.generateAndSubmitObject: testApprovalWithoutComment Fail System.QueryException: List没有分配给SObject第187行、第1列Class.Test_CustomRequireRejectionComment.testApprovalWithoutComment:第71行、第1列的行。
视图0:18 Test_CustomRequireRejectionComment testBatchRejctions failed System.DmlException: Process failed。第0行的第一个异常;第一个错误: NO_APPLICABLE_PROCESS,没有找到适用的审批过程。::[] NO_APPLICABLE_PROCESS行115,第1列。
视图0:00 Class.Test_CustomRequireRejectionComment.generateAndSubmitObject: testRejectionWithComment Fail System.QueryException: List没有分配给SObject第187行、第1列Class.Test_CustomRequireRejectionComment.testRejectionWithComment:第13行、第1列的行。
视图0:00 Class.Test_CustomRequireRejectionComment.generateAndSubmitObject: testRejectionWithoutComment Fail System.QueryException: List没有分配给SObject第187行、第1列Class.Test_CustomRequireRejectionComment.testRejectionWithoutComment:第40行、第1列的行。
发布于 2015-12-16 20:00:46
问题似乎在您的generateAndSubmitObject()静态方法中。有很多奇怪的行为。
我要一条条地把它拆开。
第1行: testIS获得一个新实例化的BMCServiceDesk__Change_Request__c对象。
第2行: testIS获取名称等于“CR00002135”的BMCServiceDesk__Change_Request__c对象列表(但只有ID字段)。这里有两个问题。首先,这个操作使第1行完全无用。第二,这不会返回任何结果。此时,您没有任何名为CR00002135的CRs是您的数据库。
第3行:将这个空列表重新插入到数据库中。这是又一次不做手术。
如果您修复了第1-3行,那么剩下的部分就可以了。我的建议是,去掉第2行和更新第1行,这样当您实例化BMCServiceDesk__Change_Request__c对象时,它就拥有了您想要的所有信息。
https://stackoverflow.com/questions/34284138
复制相似问题