0

这是一个 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 Test_CustomRequireRejectionComment testApprovalWithoutComment 失败 System.QueryException:列表没有分配给 SObject 类的行。Test_CustomRequireRejectionComment.generateAndSubmitObject:第 187 行,第 1 列 Class.Test_CustomRequireRejectionComment.testApprovalWithoutComment:第 71 行,第 1 列

[查看] 0:18 Test_CustomRequireRejectionComment testBatchRejctions Fail System.DmlException: Process failed。第 0 行的第一个异常;第一个错误:NO_APPLICABLE_PROCESS,未找到适用的审批流程。:[] Class.Test_CustomRequireRejectionComment.testBatchRejctions:第 115 行,第 1 列

[查看] 0:00 Test_CustomRequireRejectionComment testRejectionWithComment 失败 System.QueryException:列表没有分配给 SObject 类的行。Test_CustomRequireRejectionComment.generateAndSubmitObject:第 187 行,第 1 列 Class.Test_CustomRequireRejectionComment.testRejectionWithComment:第 13 行,第 1 列

[查看] 0:00 Test_CustomRequireRejectionComment testRejectionWithoutComment 失败 System.QueryException:列表没有分配给 SObject 类的行。Test_CustomRequireRejectionComment.generateAndSubmitObject:第 187 行,第 1 列 Class.Test_CustomRequireRejectionComment.testRejectionWithoutComment:第 40 行,第 1 列

4

1 回答 1

1

问题似乎出在您的 generateAndSubmitObject() 静态方法中。有很多奇怪的行为。

我将逐行分解它。

第 1 行:testIS 获取一个新实例化的 BMCServiceDesk__Change_Request__c 对象。

第 2 行:testIS 获取 BMCServiceDesk__Change_Request__c 对象列表(但只有 ID 字段),其中 Name 等于“CR00002135”。这里有两个问题。首先,此操作使第 1 行完全无用。二,这将不返回任何结果。此时您的数据库中没有任何名为 CR00002135 的 CR。

第 3 行:您正在将此空列表重新插入数据库。这是另一个无操作。

如果您修复第 1 - 3 行,其余的应该可以工作。我的建议是去掉第 2 行并更新第 1 行,这样当您实例化 BMCServiceDesk__Change_Request__c 对象时,它就会拥有您想要的所有信息。

于 2015-12-16T20:00:46.233 回答