0

Can any one help me out .Based on my requirement i have written two triggers , one on case object and one on Case comment.1)Now how many Test Classes should i write now .2)I have written a test class on case object trigger . 3)Do i need to write test class on case comment trigger.4)Writting one test class on parent object and calling the child object methods.can we write .Any suggestions or Any help is very much appreciated.

My Requirement was:

  1. Create a case with Subject line (User ) ----First Case
  2. Create another case with Same Subject line [either by (Same user or different user )] .----Second Case
  3. Now System should automatically identify it as a " Duplicate case and close the case " (i.e second case)
  4. Now the Case Comments of the Closed case (i.e second case) should be added to the existing case ( i.e first case) which is open.

Trigger on Case Object:

trigger Case_DuplicateClose on Case (before insert) {
    Map<String, Case> newCaseMap = new Map<String, Case>();
    for(Case c : trigger.new)
    {
        while(c.Subject.startsWith('RE:') || c.Subject.startsWith('FW:')|| c.Subject.startsWith('Fw:')|| c.Subject.startsWith('Re:'))
            c.Subject = c.Subject.substring(3).trim();
        if(newCaseMap.containsKey(c.Subject))
            c.Status = 'Closed';
        else
            newCaseMap.put(c.Subject, c);
    }
    for(Case c : [Select Subject From Case Where Subject in :newCaseMap.keySet()])
        newCaseMap.get(c.Subject).status = 'Closed';
}

Trigger on CaseComment Object:

trigger ReparentComment on CaseComment (before insert) {
    Map<String, Case> newCaseMap = new Map<String, Case>();
    Set<Id> caseIds = new Set<Id>();
    Map<Id, List<CaseComment>> ccMap = new Map<Id, List<CaseComment>>();
    for(CaseComment cc : trigger.new)
    {
        caseIds.add(cc.ParentId);
        List<CaseComment> ccList = ccMap.get(cc.ParentId);
        if(ccList == null)
            ccList = new List<CaseComment>();
        ccList.add(cc);
        ccMap.put(cc.ParentId, ccList);
    }
    for(Case c : [Select Id, Subject From Case Where Id in :caseIds Order by CreatedDate])
        if(newCaseMap.containsKey(c.Subject))
            for(CaseComment cc : ccMap.get(c.Id))
                cc.ParentId = newCaseMap.get(c.Subject).Id;
        else
            newCaseMap.put(c.Subject, c);

    for(Case c : [Select Id, Subject From Case Where Subject in :newCaseMap.keySet() And Id not in :caseIds And Status != 'Closed'])
        for(CaseComment cc : ccMap.get(newCaseMap.get(c.Subject).Id))
            cc.ParentId = c.Id;

}

Test Class For Case Object Trigger:

@istest 
public class TestCase_DuplicateClose {

    static testmethod void testCaseDuplicate() {

        list cases = new list();

        for (Integer i = 0; i < 200; i++) {

            Case c = new Case(Subject = 'Re:Test Case', Status = 'Open', Origin = 'Email' + i);

            c.Status = 'Closed';

            Cases.add(c);
        }

        test.startTest();

        insert cases;

        test.stopTest();

        list insertedCases = [Select Subject, Status, Origin from
            case where Id In:
                cases
        ];

        for (Case c: insertedCases) system.assertEquals('Closed', c.Status);
    }
}
3

There are no rules about how many test classes/methods you should write. The perceived goal is 100% test coverage, but of course the real goal is to achieve 100% test coverage and test your functionality.

Generally I go with one test class per source file to test to keep an easy 1:1 relationship, usually appending the prefix 'Test_' to the name of the class/trigger being tested.

Each class should test the functionality of the code being tested, and that means testing for bulk processing, using System.Assert() left right and centre, and ensuring that the code behaves as it is required to.

3
  • 1
    Also worth mentioning that you need an average test coverage of at least 75% to be able to deploy to a production environment – BarCotter Nov 6 '14 at 10:34
  • True, but if someone's asking it's generally best to tell them to aim for 100 :) – Matt Lacey Nov 6 '14 at 10:36
  • Aim for the moon. If you miss, you may hit a star. – BarCotter Nov 6 '14 at 10:36
1

You need to write two test classes that is one for each trigger. Make sure you use same name as that of triggers and post fix Test after the original Trigger name. If you use single test class it will become confusing for you or your team to figure out which Test class is being used to cover the code coverage.

0

Read the following articles regarding trigger patterns and best practices

A Simple Trigger Template for Salesforce

Trigger Pattern for Tidy, Streamlined, Bulkified Triggers

Apex Triggers the Definitive Guide

Two interesting ways to architect Apex triggers

All articles provide a cool advice regarding architecture salesforce triggers and you can mix up these recommendations for create your own trigger template.

I do believe that you have to move all code inside separate classes with small simple methods which will do only one specific thing. After that you can create a test class with small test methods which will test only one action per test method for all Case functionality, because Salesforce has no convinient namespace/package system I do not recommend to create a more classes that you really need because it'll be a reason of clutter in classes in your project.

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.