Can anybody help me please to finish this test class, I am new in testing class and I spent the whole day trying to get coverage for this trigger, so far I get 33% and looks like the only part of the code that is getting coverage is the AggregateResult.

Here is the trigger:

Trigger OSOCount on Purchase_Order__c (before update) {
    for(AggregateResult result : [
            SELECT PO_Lookback__c, SUM(Total_Qty_to_Ship__c)
            FROM Outbound_Sales_Order__c
            WHERE PO_Lookback__c IN :Trigger.new
            GROUP BY PO_Lookback__c
        Purchase_Order__c prod = Trigger.newMap.get((string)result.get('PO_Lookback__c'));
        prod.OSO_Count__c= (double)result.get('expr0');

here is the testing class I am working on, I will appreciate any help

public class Test_OSOCount 
    private static testmethod void TestOSOCount ()
        Account account1 = new Account(Name = 'Test Account1');
        insert account1;

        Purchase_Order__c  aa = new Purchase_Order__c (Account__c = account1.id);
        insert aa;
        update aa;

        update aa;

1 Answer 1


One problem is that while you do insert a Purchase_Order__c you never update it and the trigger is a "before update" trigger.

PS OK you just changed the test code. My answer relates to version 2 of the question.

PPS The test needs to wire up the objects and then update the Purchase_Order__c (assuming the trigger does what is required):

public class Test_OSOCount { 
    private Static testmethod void TestOSOCount () {

        Account a = new Account(Name = 'Test Account1');
        insert a;

        Purchase_Order__c po = new Purchase_Order__c(Account__c = a.id);
        insert po

        insert new Outbound_Sales_Order__c[] {
                new Outbound_Sales_Order__c(PO_Lookback__c = po.Id, Total_Qty_to_Ship__c = 4),
                new Outbound_Sales_Order__c(PO_Lookback__c = po.Id, Total_Qty_to_Ship__c = 9)

        update po;
        System.assertEquals(4 + 9, [
                select OSO_Count__c
                from Purchase_Order__c
                where Id = :po.Id
  • Hi Keith, so what is the best approach in this case?
    – Carlos
    Commented Apr 24, 2014 at 22:58
  • @Carlos I've added the sort of test code I would write toe the answer.
    – Keith C
    Commented Apr 24, 2014 at 23:29
  • Keith, thank you so much.. that is very clean and easy to follow. I just find my self so lost doing this test classes for triggers, would you recommend me some resources that will help me to understand this.
    – Carlos
    Commented Apr 24, 2014 at 23:47
  • @Carlos All I can suggest is that you google the keywords and read everything that comes up: I don't know of any particularly good articles on the subject. That is a pity as e.g. for Java JUnit there are whole books on testing patterns. Triggers require setup, appropriate changes and requerying to check the results; generally test examples are short on structure such as the use of builder or fixture classes.
    – Keith C
    Commented Apr 24, 2014 at 23:57

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .