0

So I'm trying to write a test class to meet code coverage requirements to push up this trigger:

trigger DeleteEventsOnCancellation on Case (after update) {
    for(Case c : system.trigger.new){
        if(c.Status == 'Cancelled')
        {
            List<Event> events = [SELECT ID FROM Event WHERE WhatID = :c.ID];
            delete events;

        }
    }


}

I'm only reaching 50% coverage with this test class and am stuck on how to get that up to 100%. The delete events call is what is being marked as uncovered. Here's the test class:

@isTest
public class testDeleteEventsOnCancellation {

    static testMethod void testEventDeletion(){

         Profile p = [SELECT Id FROM Profile WHERE Name = 'System Administrator'];

        User u = new User();
        u.FirstName = 'Test';
        u.LastName = 'User2';
...
        insert u;

        User u2 = new User();
        u2.FirstName = 'test';
        u2.LastName = 'User';
...
        insert u2;

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

        Case c = new Case();
        c.AccountId = a.Id;
        c.Status = 'New';
        c.Origin = 'Email';
        c.RecordTypeId = Case.SObjectType.getDescribe().getRecordTypeInfosByName().get('Work Order').getRecordTypeId();
        insert c;

        Event e = new Event();
        e.WhatId=c.Id;
         e.StartDateTime=datetime.now();
        e.EndDateTime=datetime.now();
        insert e;
                Event e2 = new Event();
        e2.WhatId=c.Id;
         e2.StartDateTime=datetime.now();
        e2.EndDateTime=datetime.now();
        insert e2;

        test.startTest();
        c.Status= '9-Cancelled';
        update c;
        test.stopTest();

        List<Event> events = [SELECT ID,IsDeleted FROM Event WHERE WhatID = :c.ID];
        for(Event ev:events){
            system.assertequals(ev.IsDeleted,true);
        }

    }


}

Is there something I need to do differently to cover the delete method?

  • 2
    Your trigger checks for status value Cancelled and that you are setting this value in your test class as 9-Cancelled. Is that correct? – Jayant Das Feb 6 at 17:25
3

At least with the code you have now, your test class is not covering it correctly.

You have the following in your trigger:

if(c.Status == 'Cancelled')

And that you are it as below in your test class:

 c.Status= '9-Cancelled';

To get to the condition thus covering your code, you should set the status value to Cancelled in your test class as:

c.Status= 'Cancelled';
3

In your trigger if block ,you are checking if case is canceled.

if(c.Status == 'Cancelled')

and in your test class you are setting it as 9-Cancelled and as Cancelled is not equal to 9-Cancelled , its not entering the block.

You can fix it by fixing the case status in test class.

    test.startTest();
    c.Status= 'Cancelled';
    update c;
    test.stopTest()
0

You can actually increase your code coverage simply by optimizing your code. Here's a new version of the trigger:

trigger DeleteEventsOnCancellation on Case (after update) {
    delete [SELECT Id FROM Task WHERE WhatId IN (SELECT Id FROM Case WHERE Id = :Trigger.new AND Status='Cancelled')];
}

You will, of course, need to verify that the tasks where successfully deleted, so make sure you query back and assert. Also, as the other answers state, '9-Cancelled' is not the same as 'Cancelled'.

Also, in your original code, be aware that you have a DML in a loop, which could break in large updates. You should strive to write unit tests that test at least 200 records to make sure you're not hitting governor limits.

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.