0

How to write Test Classes for the below Trigger.

trigger CloseComment on CaseComment (before insert) {

for (CaseComment t: Trigger.new)
{
    Case c = new Case(Id = t.ParentId);
    c = [SELECT Status FROM Case WHERE Id = :c.Id];

    if(c.Status == 'Closed' && System.Userinfo.getUserType()!= 'Standard')
        {
        t.addError('You cannot add comments to closed cases.');
        }
    }

I have a case created and closing that case, before closing I need to add the internal comments.

Since I am newbie, I have written something like below:

@isTest
private class CloseCommentTest {

    private static testMethod void test() {
        Test.startTest();
        Case ca = new Case(SuppliedPhone = 'XXXXXX', 
                            Origin = 'Web',
                            SuppliedName='XXXX',
                            Description ='XXXXXXXX');

        insert ca;

        List<Case> lstCase=[Select Id,Status,Reopened__c from case where Id in :ca.parentId ];
        System.debug('Case Size :'+ lstCase);

        CaseComment cc = new CaseComment(cc.Status='Closed');


        Test.stopTest();
    }   

}
3

So the first step is writing one test each that will confirm the validation and successful save paths:

static testmethod void testValidation()
{
    // set up closed case
    // set up comment in memory but do not insert
    DmlException expectedException;
    Test.startTest();
        try { /*insert comment*/ }
        catch (DmlException d) { expectedException = d; }
    Test.stopTest();
    system.assertNotEquals(null, expectedException, 'write your own message');
    system.assertEquals(0, [SELECT count() FROM CaseComment], 'write your own message');
}
static testmethod void testSuccessfulSave()
{
    // set up non-closed case
    // set up a comment with exactly the same data as the other test
    Test.startTest();
        // insert comment
    Test.stopTest();
    system.assertEquals(1, [SELECT count() FROM CaseComment], 'write your own message');
}

Now these tests should both pass. Once you've run them and confirmed as much, you can guarantee that a single comment will be validated correctly when its creation should be prevented, but also that valid comments can still be placed. You still need one more test, however, a bulk test.

static testMethod void testSuccessfulSave_Bulk();
{
    Integer count = Limits.getLimitQueries() + 1;
    // set up case
    List<CaseComment> comments = new List<CaseComment>();
    for (Integer i = 0; i < count; i++)
        comments.add(new CaseComment(/*data*/));
    Test.startTest();
        insert comments;
    Test.stopTest();
    system.assertEquals(count, [SELECT count() FROM CaseComment], 'write your own message');
}

This test should fail, because you have not properly bulkified your trigger. That topic is covered ad nauseum both here and on other forums, and you should be able to follow our General trigger bulkification - best practices to fix the problem.

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.