function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Balasubramani DhanapalBalasubramani Dhanapal 

Writing Test Class for triggerHandler?

Hi ,
I'm having trouble writing the tests class for the following Trigger Handler . I'm new to SF. Can some one please help.
Invocie Trigger Handler:
---------------------------------

Public Class InvoiceTriggerHandler{
    
    Public Void OnAfterInsert(Map<id, Invoice__c> newInvoiceMap,Map<id, Invoice__c> oldInvoiceMap){
        
    }
    
    Public Void OnAfterUpdate(Map<id, Invoice__c> newInvoiceMap,Map<id, Invoice__c> oldInvoiceMap){
        if(RecursiveHandler.runITH_POOnce()) PlaceOrder(newInvoiceMap, oldInvoiceMap);
    }
    
    Private Void PlaceOrder(Map<id, Invoice__c> newInvoiceMap, Map<id, Invoice__c> oldInvoiceMap){
        List<id> invIdToPlaceOrders=new List<Id>();
        List<id> quoteIds=new List<Id>();
        for(Invoice__c inv:newInvoiceMap.values()){
            if(inv.Generate_order__c== True  && inv.Generate_order__c!=oldInvoiceMap.get(inv.Id).Generate_order__c){
                invIdToPlaceOrders.add(inv.id);
                quoteIds.add(inv.Quote__c);
            }
        }
        Map<id, Quote> qteMap=new Map<Id, Quote>([SELECT BillingCity,BillingCountry,BillingCountryCode,BillingLatitude,BillingLongitude,BillingName,BillingPostalCode,BillingState,BillingStateCode,BillingStreet,ContactId,Contact.FirstName,Contact.LastName,Description,Discount,Email,ExpirationDate,Fax,GrandTotal,Id,LineItemCount,Name,OpportunityId,Phone,Pricebook2Id,QuoteNumber,ShippingCity,ShippingCountry,ShippingCountryCode,ShippingHandling,ShippingLatitude,ShippingLongitude,ShippingName,ShippingPostalCode,ShippingState,ShippingStateCode,ShippingStreet,Status,Subtotal,Tax,TotalPrice,Dead_Line__c FROM Quote where id IN:quoteIds]);        
        List<Order> listOrderToInsert=new List<Order>();
        Order temp;
        Invoice__c inv;
        Quote qte;
        for(id invId:invIdToPlaceOrders){
            temp=new Order();
            inv=newInvoiceMap.get(invId);
            qte=qteMap.get(inv.Quote__c);
            temp.QuoteId=inv.Quote__c;
            temp.AccountId=inv.AccountID__c;
            temp.OpportunityId=inv.Opportunity__c;
            temp.billTocontactId=qte.ContactID;
            temp.billingStreet=qte.billingStreet;
            temp.billingCity=qte.billingCity;
            temp.billingState=qte.billingState;
            temp.billingCountry=qte.billingCountry;
            temp.BillingPostalCode=qte.BillingPostalCode;
            
            temp.ShippingStreet=qte.ShippingStreet;
            temp.ShippingCity=qte.ShippingCity;
            temp.ShippingState=qte.ShippingState;
            temp.ShippingCountry=qte.ShippingCountry;
            temp.ShippingPostalCode=qte.ShippingPostalCode; 
            temp.EffectiveDate=System.today();
            temp.Status='Initial Progress';
            temp.Invoice__c=invId;
            temp.Pricebook2Id=qte.Pricebook2Id;
            temp.EndDate=qte.Dead_Line__c;
            listOrderToInsert.add(temp);
        }
        if(listOrderToInsert.size()>0)
            insert listOrderToInsert;
    }
    
}
Thanks in advance.
pconpcon
Since writing a test class to cover all of the facets of this class is not something that anyone on here will do for you, I can give you some pointers and hopefully get you started.  I would recommend that you do some reading on testing [1] [2] [3] to get a better understanding.  Each of your individual tests should only tests one specific portion of you class (ie a constructor test, sendEmail test, contactSelected test, etc).  You should also have both a postitive (everything works perfectly) and a negative (things are not right) test.

In your case, you'll want to insert your Invoice__c records and then call your class methods.  I would however recommend that you test this as a trigger instead of testing it directly.  So you would have your insert/update inside of your Test.startTest/stopTest.

Each test should follow the following structure:
  • Setup of test data. This includes creation of any data needed by your class.  Account, Contacts etc
  • Starting the test. This is calling Test.startTest() to reset the governor limits
  • Calling your class / method
  • Stopping the test.This is calling Test.stopTest() to reset the governor limits and allow for any async jobs to finish
  • Asserting that your changes have worked
    • If you have inserted/updated/deleted data, you need to query for the updates
    • Run System.assert, System.assertEquals, System.assertNotEquals to verify that you got the correct data back
If you have any specific problems with your tests, feel free to create a new post with the part of the class you are trying to test and your current test method, and you will more likely get a better response then asking for someone to essentially write an entire test class for you.

[1] http://www.sfdc99.com/2013/05/14/how-to-write-a-test-class/
[2] http://pcon.github.io/presentations/testing/
[3] http://blog.deadlypenguin.com/blog/2014/07/23/intro-to-apex-auto-converting-leads-in-a-trigger/