+ Start a Discussion
revathyrevathy 

Test class for apex approval process?getting error

System.AssertException: Assertion Failed: Instance StatusPending: Expected: Approved, Actual: Pending

My Test Class
========
@isTest(SeeAllData=true)
public class GE_LGT_EM_ApproveReject_test{
static testMethod void myUnitTest() {
    // Insert an account
        Case Caseobj=new Case(GE_LGT_EM_Salesorg__c='2BE1',Subject='test sample data');
        insert Caseobj;
       
       
        GE_LGT_EM_InvoiceLineItem__c newrec=new GE_LGT_EM_InvoiceLineItem__c(Name='testinvoicedata',CurrencyIsoCode='USD',GE_LGT_EM_CM_DM_RO_NO__c='5000');
        insert newrec;      
       
           
        GE_LGT_EM_ComplaintLineItem__c acc  = new GE_LGT_EM_ComplaintLineItem__c(GE_LGT_EM_Category__c='118-Product Mispick-Return',GE_LGT_EM_Comments__c='sample data',GE_LGT_EM_CaseNumber__c=Caseobj.id,CurrencyIsoCode='USD',GE_LGT_EM_CM_DM_RO_Number__c='500');
        insert acc ;
       
       Test.setMock(WebServiceMock.class, new GE_LGT_EM_171WebServiceMockImpl_test());
         // Create an approval request for the cli
    Approval.ProcessSubmitRequest req1 =new Approval.ProcessSubmitRequest();
    req1.setComments('Submitting request for approval.');
    req1.setObjectId(acc.id);

    // Submit the approval request for the cli
    Approval.ProcessResult result = Approval.process(req1);

    // Verify the result
    System.assert(result.isSuccess());

    System.assertEquals(
        'Pending', result.getInstanceStatus(),
        'Instance Status'+result.getInstanceStatus());
       
       
  // Approve the submitted request
    // First, get the ID of the newly created item
    List<Id> newWorkItemIds = result.getNewWorkitemIds();

    // Instantiate the new ProcessWorkitemRequest object and populate it
    Approval.ProcessWorkitemRequest req2 =
        new Approval.ProcessWorkitemRequest();
    req2.setComments('Approving request.');
    req2.setAction('Approve');
    req2.setNextApproverIds(new Id[] {UserInfo.getUserId()});

    // Use the ID from the newly created item to specify the item to be worked
    req2.setWorkitemId(newWorkItemIds.get(0));

    // Submit the request for approval
    Approval.ProcessResult result2 =  Approval.process(req2);

    // Verify the results
   
   

   
    System.assert(result2.isSuccess(), 'Result Status:'+result2.isSuccess());

    System.assertEquals('Approved', result2.getInstanceStatus(),   //here getting error
      'Instance Status'+result2.getInstanceStatus());     

      
  
}    
       
   
}
}
please help me on this

James LoghryJames Loghry
I would wrap your Approval processing (anything to do with creating / processing the approval requests) inside of Test.startTest() and Test.stopTest() calls.  Then you would move your System.assertions after the Test.stopTest call.  That way, your assertions run after any asynchronous calls, which may be way your instance status still shows Pending when it should say Approved.  Another thing you'll want to look at is using "System.runAs(User)" to ensure the approval process is run as expected for a certain type of User.  This is especially important for any User / Profile specific approval processes you may have.
revathyrevathy
Hi James,
after i added Test.startTest() and Test.stopTest() still getting same error,please help me more
@isTest(SeeAllData=true)
public class GE_LGT_EM_ApproveReject_test{
static testMethod void myUnitTest() {
   
    Test.startTest();
        Case Caseobj=new Case(GE_LGT_EM_Salesorg__c='2BE1',Subject='test sample data');
        insert Caseobj;
       
       
        GE_LGT_EM_InvoiceLineItem__c newrec=new GE_LGT_EM_InvoiceLineItem__c(Name='testinvoicedata',CurrencyIsoCode='USD',GE_LGT_EM_CM_DM_RO_NO__c='5000');
        insert newrec;      
       
           
        GE_LGT_EM_ComplaintLineItem__c acc  = new GE_LGT_EM_ComplaintLineItem__c(GE_LGT_EM_Category__c='118-Product Mispick-Return',GE_LGT_EM_Comments__c='sample data',GE_LGT_EM_CaseNumber__c=Caseobj.id,CurrencyIsoCode='USD',GE_LGT_EM_CM_DM_RO_Number__c='500');
        insert acc ;
       
       Test.setMock(WebServiceMock.class, new GE_LGT_EM_171WebServiceMockImpl_test());
         // Create an approval request for the cli
    Approval.ProcessSubmitRequest req1 =new Approval.ProcessSubmitRequest();
    req1.setComments('Submitting request for approval.');
    req1.setObjectId(acc.id);

    // Submit the approval request for the cli
    Approval.ProcessResult result = Approval.process(req1);
Test.stopTest();
    // Verify the result
    System.assert(result.isSuccess());

    System.assertEquals(
        'Pending', result.getInstanceStatus(),
        'Instance Status'+result.getInstanceStatus());
       
       
  // Approve the submitted request
    // First, get the ID of the newly created item
    List<Id> newWorkItemIds = result.getNewWorkitemIds();

    // Instantiate the new ProcessWorkitemRequest object and populate it
    Approval.ProcessWorkitemRequest req2 =
        new Approval.ProcessWorkitemRequest();
    req2.setComments('Approving request.');
    req2.setAction('Approve');
    req2.setNextApproverIds(new Id[] {UserInfo.getUserId()});

    // Use the ID from the newly created item to specify the item to be worked
    req2.setWorkitemId(newWorkItemIds.get(0));

    // Submit the request for approval
    Approval.ProcessResult result2 =  Approval.process(req2);

    // Verify the results
system.debug('--------->'+result2);
    System.assert(result2.isSuccess(), 'Result Status:'+result2.isSuccess());

    System.assertEquals('Approved', result2.getInstanceStatus(),
      'Instance Status'+result2.getInstanceStatus());     
      
  
}    
       
   
}