1

I have a trigger and I have also written a test class to test the trigger but only 20% of the trigger code is covered leaving 134 lines uncovered. My overall coverage is 60% and I'm trying to increase the coverage to 75%.

I am posting the trigger and the test class I've written. Can someone please help me.

TRIGGER

trigger ShowingNotesAdded on pb__Showing__c (after insert, before update) {
Account[] a = new Account[6];
pb__InventoryItem__c[] i = new pb__InventoryItem__c[6];
integer index = 0;
integer c = 0;
string response;
Recordtype r;
Date duedate;
String[] toAddresses = new String[] {};
for (pb__Showing__c s : trigger.new){
    //r = [select name from recordtype where sobjecttype = 'pb__Showing__c' and id = :s.RecordTypeId];


   if(trigger.IsInsert){

        response = s.record_type_name__c;
        if (response.contains('Lettings')){
            r = [select id, Name from recordtype where sobjecttype = 'Event' and Name = 'Lettings Viewing'];
        }
        if (response.contains('Sales')){
            r = [select id, Name from recordtype where sobjecttype = 'Event' and Name = 'Sales Viewing'];
        }  
        if (response.contains('Lease')){
            r = [select id, Name from recordtype where sobjecttype = 'Event' and Name = 'Lease Viewing'];
        }  

        Event e = new Event();
        e.OwnerId = s.OwnerId;
        e.Subject = r.Name;
        e.RecordTypeId = r.Id;
        e.Description = 'Event created through Showing/Viewing';
        e.WhatId = s.Id;
        e.StartDateTime = s.start_time__c;
        e.EndDateTime = s.end_time__c;
        insert e;
  }


    if (trigger.IsUpdate){
        pb__Showing__c olds = Trigger.oldMap.get(s.ID);
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();

        //create date from datetime
        duedate = Date.newInstance(s.Start_Time__c.year(),s.Start_Time__c.month(),s.Start_Time__c.day());

        if (s.pb__Inventory__c != null && s.Inventory_A_Notes__c != null && s.Send_Feedback_to_Vendor_A__c != false ){
            toAddresses.clear();
            i[index] = [select id, Unit_No__c, AreaUAE__c, Project__c, sellerid__c from pb__InventoryItem__c where id =: s.pb__Inventory__c];
            a[index] = [select Id, Name, PersonEmail from Account where Id =: i[index].SellerId__c];

            mail.setPlainTextBody('Dear ');
            toAddresses.add(a[index].PersonEmail);
            mail.setToAddresses(toAddresses);                             
            mail.setUseSignature(false);
            mail.setSenderDisplayName('Smith & Ken');                           
            mail.setSubject('Viewing Feedback');

            try{
                Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
                }
            catch(System.EmailException ex){
                system.debug('============== email exception caught!!!=============');
                response = ex.getMessage();
            }    
            Task t = new Task();
            t.OwnerId = s.OwnerId;
            t.Subject = 'Viewing Feedback';
            t.Description = s.Inventory_A_Notes__c;
            t.WhatId = i[index].Id;
            t.ActivityDate = duedate;
            t.Status = 'Completed';

            insert t;
            index++;
        }

       if (s.Inventory_B__c != null && s.Inventory_B_Notes__c != null  && s.Send_Feedback_to_Vendor_B__c != false ){
            toAddresses.clear();
            i[index] = [select id, Unit_No__c, AreaUAE__c, Project__c, sellerid__c from pb__InventoryItem__c where id =: s.Inventory_B__c];
            a[index] = [select Id, Name,PersonEmail from Account where Id =: i[index].SellerId__c];

            mail.setPlainTextBody('Dear ');
            toAddresses.add(a[index].PersonEmail);
            mail.setToAddresses(toAddresses);                             
            mail.setUseSignature(false);
            mail.setSenderDisplayName('Smith & Ken');                           
            mail.setSubject('Viewing Feedback');

            try{
                Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
                }
            catch(System.EmailException ex){
                system.debug('============== email exception caught!!!=============');
                response = ex.getMessage();
            }
            Task t = new Task();
            t.OwnerId = s.OwnerId;
            t.Subject = 'Viewing Feedback';
            t.Description = s.Inventory_B_Notes__c;
            t.ActivityDate = duedate;
            t.WhatId = i[index].Id;
            t.Status = 'Completed';

            insert t;
            index++;
        }

        if (s.Inventory_C__c != null && s.Inventory_C_Notes__c != olds.Inventory_C_Notes__c && s.Send_Feedback_to_Vendor_C__c != false ){
            toAddresses.clear();
            i[index] = [select id, Unit_No__c, AreaUAE__c, Project__c, sellerid__c from pb__InventoryItem__c where id =: s.Inventory_C__c];
            a[index] = [select Id, Name,PersonEmail from Account where Id =: i[index].SellerId__c];

            mail.setPlainTextBody('Dear');
            toAddresses.add(a[index].PersonEmail);
            mail.setToAddresses(toAddresses);                             
            mail.setUseSignature(false);
            mail.setSenderDisplayName('Smith & Ken');                           
            mail.setSubject('Viewing Feedback');

            try{
                Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
                }
            catch(System.EmailException ex){
                system.debug('============== email exception caught!!!=============');
                response = ex.getMessage();
            }
            Task t = new Task();
            t.OwnerId = s.OwnerId;
            t.Subject = 'Viewing Feedback';
            t.ActivityDate = duedate;
            t.Description = s.Inventory_C_Notes__c;
            t.WhatId = i[index].Id;
            t.Status = 'Completed';

            insert t;
            index++;
        }

        if (s.Inventory_D__c != null && s.Inventory_D_Notes__c != olds.Inventory_D_Notes__c && s.Send_Feedback_to_Vendor_D__c != false ){
            toAddresses.clear();
            i[index] = [select id, Unit_No__c, AreaUAE__c, Project__c, sellerid__c from pb__InventoryItem__c where id =: s.Inventory_D__c];
            a[index] = [select Id, Name,PersonEmail from Account where Id =: i[index].SellerId__c];

            mail.setPlainTextBody('Dear');
            toAddresses.add(a[index].PersonEmail);
            mail.setToAddresses(toAddresses);                             
            mail.setUseSignature(false);
            mail.setSenderDisplayName('Smith & Ken');                           
            mail.setSubject('Viewing Feedback');

            try{
                Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
                }
            catch(System.EmailException ex){
                system.debug('============== email exception caught!!!=============');
                response = ex.getMessage();
            }
            Task t = new Task();
            t.OwnerId = s.OwnerId;
            t.ActivityDate = duedate;
            t.Subject = 'Viewing Feedback';
            t.Description = s.Inventory_D_Notes__c;
            t.WhatId = i[index].Id;
            t.Status = 'Completed';

            insert t;
            index++;
        }

        if (s.Inventory_E__c != null && s.Inventory_E_Notes__c != olds.Inventory_E_Notes__c && s.Send_Feedback_to_Vendor_E__c != false){
            toAddresses.clear();
            i[index] = [select id, Unit_No__c, AreaUAE__c, Project__c, sellerid__c from pb__InventoryItem__c where id =: s.Inventory_E__c];
            a[index] = [select Id, Name,PersonEmail from Account where Id =: i[index].SellerId__c];

            mail.setPlainTextBody('Dear');
            toAddresses.add(a[index].PersonEmail);
            mail.setToAddresses(toAddresses);                             
            mail.setUseSignature(false);
            mail.setSenderDisplayName('Smith & Ken');                           
            mail.setSubject('Viewing Feedback');

            try{
                Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
                }
            catch(System.EmailException ex){
                system.debug('============== email exception caught!!!=============');
                response = ex.getMessage();
            }
           Task t = new Task();
            t.OwnerId = s.OwnerId;
            t.Subject = 'Viewing Feedback';
            t.ActivityDate = duedate;
            t.Description = s.Inventory_E_Notes__c;
            t.WhatId = i[index].Id;
            t.Status = 'Completed';

            insert t;
            index++;
        }

        if (s.Inventory_F__c != null && s.Inventory_F_Notes__c != olds.Inventory_F_Notes__c && s.Send_Feedback_to_Vendor_F__c != false){
            toAddresses.clear();
            i[index] = [select id, Unit_No__c, AreaUAE__c, Project__c, sellerid__c from pb__InventoryItem__c where id =: s.Inventory_F__c];
            a[index] = [select Id, Name,PersonEmail from Account where Id =: i[index].SellerId__c];

            mail.setPlainTextBody('Dear');
            toAddresses.add(a[index].PersonEmail);
            mail.setToAddresses(toAddresses);                             
            mail.setUseSignature(false);
            mail.setSenderDisplayName('Smith & Ken');                           
            mail.setSubject('Viewing Feedback');

            try{
                Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
                }
            catch(System.EmailException ex){
                system.debug('============== email exception caught!!!=============');
                response = ex.getMessage();
            }

            Task t = new Task();
            t.OwnerId = s.OwnerId;
            t.Subject = 'Lettings Viewing Feedback';
            t.Description = s.Inventory_F_Notes__c;
            t.ActivityDate = duedate;
            t.WhatId = i[index].Id;
            t.Status = 'Completed';
            insert t;
            index++;
        }
    }
}

}

Test Class for the trigger

@isTest private class UnitTestShowingNotesAdded {

static testMethod void myUnitTest() {
    // TO DO: implement unit test




    User u = [select id from user where Name = 'Benjamin Smith'];

    //create test account
    Account a = new Account();
    a.LastName = 'testabcdef';
    a.PersonEmail='test@testsnk.com';
    a.Phone='987654';
    a.OwnerId = u.id;
    a.service_breach__c = false;
    a.pb__Status__c = 'Unqualified';
    insert a;

   pb__InventoryItem__c inv = new pb__InventoryItem__c();
    inv.name = 'abc123';
    inv.pb__IsForSale__c = true;
    inv.pb__PurchaseListPrice__c = 12313;
    inv.pb__IsAvailable__c = true;
    inv.Project__c = 'abcdd';
    inv.pb__ItemStatus__c = 'Available';
    inv.pb__IsBlocked__c = false;
    inv.SellerId__c = a.id;
    inv.AreaUAE__c = 'Dubai Marina';
    inv.Unit_No__c = '12test';
    inv.ItemStage__c = 'Available';
    insert inv;


   inv.OwnerId = u.id;
   update inv;

   //B

   pb__InventoryItem__c inv1 = new pb__InventoryItem__c();
    inv1.name = 'abc123';
    inv1.pb__IsForSale__c = true;
    inv1.pb__PurchaseListPrice__c = 12313;
    inv1.pb__IsAvailable__c = true;
    inv1.Project__c = 'abcdd';
    inv1.pb__ItemStatus__c = 'Available';
    inv1.pb__IsBlocked__c = false;
    inv1.SellerId__c = a.id;
    inv1.AreaUAE__c = 'Dubai Marina';
    inv1.Unit_No__c = '12test';
    inv1.ItemStage__c = 'Available';
    insert inv1;


   inv1.OwnerId = u.id;
   update inv1;


   pb__InventoryItem__c inv2 = new pb__InventoryItem__c();
    inv2.name = 'abc123';
    inv2.pb__IsForSale__c = true;
    inv2.pb__PurchaseListPrice__c = 12313;
    inv2.pb__IsAvailable__c = true;
    inv2.Project__c = 'abcdd';
    inv2.pb__ItemStatus__c = 'Available';
    inv2.pb__IsBlocked__c = false;
    inv2.SellerId__c = a.id;
    inv2.AreaUAE__c = 'Dubai Marina';
    inv2.Unit_No__c = '12test';
    inv2.ItemStage__c = 'Available';
    insert inv2;


   inv2.OwnerId = u.id;
   update inv2;

   Datetime todays= datetime.now();

     pb__Showing__c s = new pb__Showing__c();
     s.Start_Time__c=todays;
     s.End_Time__c=todays.addHours(2);
     s.Account__c=a.id;
     s.pb__Inventory__c=inv2.id;
     s.Status__c = 'Pending';
     insert s;




}

}

THIS IS THE CODE THAT ISN'T BEING COVERED.

i[index] = [select id, Unit_No_c, AreaUAE_c, Project_c, sellerid_c from pb_InventoryItem_c where id =: s.pb_Inventory_c]; a[index] = [select Id, Name, PersonEmail from Account where Id =: i[index].SellerId__c];

        mail.setPlainTextBody('Dear ');
        toAddresses.add(a[index].PersonEmail);
        mail.setToAddresses(toAddresses);                             
        mail.setUseSignature(false);
        mail.setSenderDisplayName('Smith & Ken');                           
        mail.setSubject('Viewing Feedback');

        try{
            Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
            }
        catch(System.EmailException ex){
            system.debug('============== email exception caught!!!=============');
            response = ex.getMessage();
        }    
        Task t = new Task();
        t.OwnerId = s.OwnerId;
        t.Subject = 'Viewing Feedback';
        t.Description = s.Inventory_A_Notes__c;
        t.WhatId = i[index].Id;
        t.ActivityDate = duedate;
        t.Status = 'Completed';

        insert t;
        index++;
    }
  • Could you at the very least indicate what is not covered and what you suspect to be the problem ? – Samuel De Rycke Jan 13 '14 at 9:28
  • Hi Mate, the query to send the email and create a task is not being covered from LINE i[index] = [select id, Unit_No__c, AreaUAE__c, Project__c, sellerid__c from pb__InventoryItem__c where id =: s.Inventory_B__c]; – user4675 Jan 13 '14 at 9:43
  • Hi Mate, I have added the code which isn't getting covered to the question above. Thanks – user4675 Jan 13 '14 at 9:57
5

The trigger is set to fire on after Insert and before Update, in your test class you look to be only calling an Insert on your pb__Showing_c object therefore the lines wrapped in the if (trigger.IsUpdate) { } section will not get covered by this test.

To gain any coverage in this section you will need to write test code to call an Update on a pb_Showing_c object.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.