Thursday 3 April 2014

Best Apex Test Class Example for Beginners in Salesforce

Trigger :

  1. trigger RestrictInvoiceDeletion on Invoice_Statement__c (before delete)
  2. {
  3.     // With each of the invoice statements targeted by the trigger and that have line items, add an error to prevent them from being deleted.
  4.     for (Invoice_Statement__c invoice :[SELECT Id
  5.                                         FROM Invoice_Statement__c WHERE Id IN (SELECT Invoice_Statement__c FROM Line_Item__c) AND Id IN :Trigger.old])
  6.     {
  7.         Trigger.oldMap.get(invoice.Id).addError('Cannot delete invoice statement with line items');
  8.     }
  9. }

Line_Item__c is the Junction Object
Invoice_Statement__c  is the Master Detail Relationship


Screenshot for the Relationship




Test Data Factory

Create a Test Data factory class instead of every time creating records in test class.

  1. @isTest
  2. public class TestDataFactory
  3. {
  4.     public static Invoice_Statement__c createInvoice(boolean inv)
  5.     {
  6.         Invoice_Statement__c invoice=createInvoice();
  7.        
  8.         if(inv==true)
  9.         {
  10.             Merchandise__c me=createMerchandise();
  11.             createLineItem(invoice,me);
  12.         }
  13.        
  14.         return invoice;
  15.     }
  16.    
  17.     private static Merchandise__c createMerchandise()
  18.     {
  19.         Merchandise__c mer=new Merchandise__c(Name='Test',Description__c='test description',Price__c=1000,Total_Inventory__c=10);
  20.         insert mer;
  21.         return mer;
  22.     }
  23.    
  24.     private static Invoice_Statement__c createInvoice()
  25.     {
  26.         Invoice_Statement__c invRec=new Invoice_Statement__c(Description__c='test description');
  27.         insert invRec;
  28.         return invRec;
  29.     }
  30.    
  31.     private static Line_Item__c createLineItem(Invoice_Statement__c inv,Merchandise__c mer)
  32.     {
  33.         Line_Item__c li=new Line_Item__c(Invoice_Statement__c=inv.id,Merchandise__c=mer.Id,Unit_Price__c=mer.Price__c);
  34.         insert li;
  35.         return li;
  36.     }
  37. }

Test Class
  1. @isTest
  2. private class RestrictInvoiceDeleteTest
  3. {
  4.     static testMethod void nonDeletedData()
  5.     {
  6.         Invoice_Statement__c inv=TestDataFactory.createInvoice(true);
  7.         Test.startTest();
  8.        
  9.         Database.DeleteResult delRes=Database.Delete(inv,false);
  10.        
  11.         Test.stopTest();
  12.        
  13.         System.assert(!delRes.isSuccess());
  14.        
  15.        
  16.     }
  17.     static testMethod void deletedData()
  18.     {
  19.         Invoice_Statement__c inv=TestDataFactory.createInvoice(false);
  20.         Test.startTest();
  21.        
  22.         Database.DeleteResult delRes=Database.Delete(inv,false);
  23.        
  24.         Test.stopTest();
  25.        
  26.         System.assert(delRes.isSuccess());
  27.        
  28.     }
  29.    
  30.     static testMethod void testBulkDelete()
  31.     {
  32.         List<Invoice_Statement__c> invList=new List<Invoice_Statement__c>();
  33.         invList.add(TestDataFactory.createInvoice(true));
  34.         invList.add(TestDataFactory.createInvoice(false));
  35.        
  36.         Test.startTest();
  37.         Database.DeleteResult[] delRec=Database.Delete(invList,false);
  38.         Test.stopTest();
  39.        
  40.         System.assert(!delRec[0].isSuccess());
  41.         System.assert(!delRec[1].isSuccess());
  42.        
  43.     }
  44. }

Note: The above example provided by salesforce only. This example is very useful for beginners. If you have a doubts on this example please comment it.. I will help for you...!


Thanks for reading this post...! Hope this will helpful for you.........! 


4 comments:

  1. Hi

    Would you pls post the same using Lead and Campaign sObjects

    Thanks
    suresh.vempally@gmail.com

    ReplyDelete
  2. hi Arun,why we use start test and stop test?
    plx explain

    ReplyDelete
  3. @Anji,

    1. If you want to test your schedule, batch classes you need the test within the startTest() and stopTest(), Because within this method the process will be asynchronous.

    2. This method allocate separate governor limit within the same method.


    For more info Refer the below link ,
    https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_testing_tools_start_stop_test.htm

    ReplyDelete
  4. I have found great and massive information. Thanks for sharing
    Salesforce CPQ Online Training
    CPQ Salesforce Training

    ReplyDelete

Activities: Assign Tasks to a Queue Salesforce Lightning

Salesforce announced to assign Tasks to a Queue beginning from Spring'20 release. How does it work? In Setup, enter Queues in th...