0

enter image description here Can any one help me out with this Test class written for this trigger is right or wrong.The Test class get saved but unable to see the code coverage.

Here is the Code for the TRIGGER :

trigger CaseTrigger on Case (after insert) {

Map<String, Map<Id, Map<Case, Map<Id, CaseComment>>>> subjectCaseCommentsMap = new Map<String, Map<Id, Map<Case, Map<Id, CaseComment>>>>();
Map<String, Id> subjectOpenCaseId = new Map<String, Id>();
Map<Id, Case> casesToUpdate = new Map<Id, Case>();
List<CaseComment> commentsToInsert = new List<CaseComment>();

// Gather the subjects
for (Case c : trigger.New) {

    if (!subjectCaseCommentsMap.containsKey(c.Subject)) {

        subjectCaseCommentsMap.put(c.Subject, new Map<Id, Map<Case, Map<Id, CaseComment>>>());
    }
}

// Gather all Cases with those subjects & their related CaseComments
for (Case c : [ SELECT
                    Id,
                    Subject,
                    Description,
                    (
                        SELECT
                            Id,
                            CommentBody,
                            ParentId
                        FROM
                            CaseComments
                    )
                FROM
                    Case
                WHERE
                    Subject IN :subjectCaseCommentsMap.keySet()
                AND
                    IsClosed = FALSE
                ORDER BY
                    CreatedDate ASC ]) {

    // Add this Case to the map
    if (!subjectCaseCommentsMap.get(c.Subject).keySet().contains(c.Id)) {

        subjectCaseCommentsMap.get(c.Subject).put(c.Id, new Map<Case, Map<Id, CaseComment>>());
    }

    subjectCaseCommentsMap.get(c.Subject).get(c.Id).put(c, new Map<Id, CaseComment>());

    // Take note of the first Case for each subject
    if (subjectOpenCaseId.get(c.Subject) == null) {

        subjectOpenCaseId.put(c.Subject, c.Id);
    }
    system.debug('Line 52: Found a Case ' + c.Subject + '. Related comments: ' + c.CaseComments);
    // Add related CaseComments to the map
    if (!c.CaseComments.isEmpty()) {

        for (CaseComment com : c.CaseComments) {

            subjectCaseCommentsMap.get(c.Subject).get(c.Id).get(c).put(com.Id, com);
        }
    }
}

// Now see if any of those Cases need to be closed
for (String sub : subjectCaseCommentsMap.keySet()) {

    // Only proceed if there are duplicates of this subject
    if (subjectCaseCommentsMap.get(sub).size() > 1) {

        for (Id cId : subjectCaseCommentsMap.get(sub).keySet()) {

            for (Case c : subjectCaseCommentsMap.get(sub).get(cId).keySet()) {

                // Is this Case the first Case for this subject?
                if (subjectOpenCaseId.get(sub) == c.Id) {

                    // Do nothing, this is the first Case for this subject

                } else {

                    // Close this Case, and clone the CaseComments
                    c.Status = 'Closed';

                    casesToUpdate.put(c.Id, c);
                    system.debug('Line 84: Closing Case ' + c.Subject + '. Related comments: ' + c.CaseComments);
                    for (CaseComment com : c.CaseComments) {

                        commentsToInsert.add(
                            new CaseComment(
                                CommentBody = com.CommentBody,

                                ParentId = subjectOpenCaseId.get(sub)
                            )
                        );
                    }
                }
            }
        }
    }
}

if (!casesToUpdate.values().isEmpty()) {

    update casesToUpdate.values();
}

if (!commentsToInsert.isEmpty()) {

    insert commentsToInsert;
}

}

****Here is the Code for the TestClass :****

@istest public with sharing class CaseTriggerTest{
    public static testmethod void testCaseTrigger()
    {
        //When a new case (First Case) is created 
        case cs = new case();
        cs.Subject= 'Testing for the Duplicate Cases';
        cs.Origin='Email';
        cs.Description = 'Email to case premium app ';
        cs.Status ='Open';
        //cs.Comments = 'cs.Description';  
        insert cs;

        casecomment ccom = new casecomment ();
        ccom .commentBody = cs.Description;
        ccom.parentid = cs.id; 
        insert ccom;
    }

    //When a new case (Second Case) is created ,it should be inserted first .
    //If it has the Same subject line ,then it should be identified as a Duplicate case and close the case.
    //By Adding its case comment to the Original case

    public static testmethod void testCaseTrigger1()
    {
        case cs1 = new case();
        cs1.Subject= 'Testing for the Duplicate Cases';
        cs1.Origin='Email';
        cs1.Description = 'Email to case premium app1 ';
        cs1.Status ='open';
        //cs.Comments = cs.Description;

        insert cs1;

        casecomment ccom1 = new casecomment ();
        ccom1 .commentBody = cs1.Description;
        ccom1.parentid = cs1.id; 

        insert ccom1;

        if(cs1.Subject == 'cs.Subject')
        {
            //cs1.Status ='open';
        //}
        //else {
            cs1.Status ='closed';
            //cs1.ccom1 .commentBody .add(ccom .commentBody);
        }

        update cs1;
    }    
}

I Shall appreciate your Help.Thanks in Advance.

3
  • So basicly your true question is 'How to see the code coverage and verify the completeness of a test class' right ? Commented Sep 11, 2014 at 9:45
  • @Samuel De Rycke :That's right ,i would like to see the code coverage and test class written is perfect.
    – nikkey
    Commented Sep 11, 2014 at 9:58
  • @Samuel De Rycke,@Prady : I tried it once again :The Overall Code Coverage is Only 27% ,but it should be 75%.How to acheive the code .This is the first test class i have in my DE.
    – nikkey
    Commented Sep 11, 2014 at 10:55

2 Answers 2

3

First of all, you should mark this question as answered seeing as you're using the code I wrote for you: Find Duplicate case and close

Secondly you need to create two cases (with comments) with the same subject in your test class - this should cover all the functionality in the trigger.

Remember to code your test class so that it is asserting that the trigger is functioning correctly and not just achieving 75% coverage.

here is a decent blog post on writing unit tests.

1
  • I love sentence#1 !
    – cropredy
    Commented Sep 11, 2014 at 18:07
2

You will need to run the test class to see the coverage. To run a test, use any of the following:

  • The Salesforce user interface
  • The Force.com IDE
  • The Force.com
  • Developer Console The API

You can view a very detailed explanation of how to go about doing it under each method here https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_testing_unit_tests_running.htm

I generally use Developer console

In the Developer Console, click Test | New Run. Classes with test methods are listed in the Select Tests window

Select classes in the Test Classes pane and click > to move the classes to the Selected Test Classes pane. To filter the list of classes, type in the Filter box. To select multiple adjacent classes, select a class and press the SHIFT key while you click the classes you want to select. To select nonadjacent classes, select a single class and press the CTRL key while you click the other classes you want to select. You can also click Select All. When all the classes you want to run are included in the Selected Test Classes pane, click Run to queue and run the classes. The test run will appear in the Tests tab. To stop a test, click Test | Abort.

You can view the test results and code coverage at the bottom panes of the developer console under test

References

https://help.salesforce.com/apex/HTViewHelpDoc?id=code_dev_console_test_runs_creating.htm&language=en_US

https://help.salesforce.com/apex/HTViewHelpDoc?id=code_run_tests.htm&language=en_US UPDATE: Screenshot of where you can find the code coverage results

Developer console

Regarding why your coverage is low, Its very difficult to identify for us without knowing which lines are not covered. You can get a view on developer console on which lines are not covered if you have the trigger opened there.

Generally you would not be having a code coverage if you dont have the data satisfying all the flow paths of your trigger. You can write multiple methods to to follow each flow path of your code. For eg. If you have a if ..else syntax in your code. one test method can handle data for the if condition is true and another for the else path.

In your case, you need have a subject field filled up in test class, You also need to have the case status also filled in so that the isclosed field is set as false. You would also need to create another case with the same subject. Again its very difficult for us to identify why the code coverage is less without knowing whats not covered. If there is a particular block of code which is not covered let us know, we can probably help you

4
  • :I'm doing the same ,when testing in Apex Test Execution : In the Detail ,Duration,Class,Method etc the information is not getting display.Similarly when tried in DC the test log :the overall code coverage is not getting displayed.
    – nikkey
    Commented Sep 11, 2014 at 10:46
  • Were you not able to view the code coverage results on the botton right panel under tests? Can you show a screenshot?
    – Prady
    Commented Sep 11, 2014 at 17:58
  • :i have made changes in my code but it still covers only 62% of code coverage.Im loosing at case comment or somewhere else .Dont Know were im loosing the loop.
    – nikkey
    Commented Sep 12, 2014 at 11:43
  • @nikkey , DavinC has given you in his answer what needs to be done. You need to create 2 cases with comments and those 2 cases need to have the same subject. Pls have the question marked as answered if DavinC answer resolved your issue
    – Prady
    Commented Sep 15, 2014 at 5:54

You must log in to answer this question.

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