Reply
Contributor
nGenxSystems
Posts: 3
0

Code Coverage - Apex unit testing and deployment

I have tried unsuccessfully to get my apex report into a deployable state. I have attempted multiple times to write unit tests that pass the tests but still end up with 0% code coverage. If someone could look at the below code and provide some advice/guidance I would greatly appreciate it. Thanks so much!

 

 

public class IdleCaseReport {
 public static void SendReport() {

     /* get group=Queue */
     List<Group> grps = [select Id,Name from Group where Type='Queue'];
     Map<Id,String> grpmap = new Map<Id,String>();
     for (Group g : grps) {
        grpmap.put(g.Id, g.Name);
     }

 

     /* get case history */
     List<CaseHistory> hist = [select CaseId,CreatedDate from CaseHistory where
                                                    Field='Owner' order by CreatedDate desc];
     Map<Id,Datetime> histmap = new Map<Id,Datetime>();


     for (CaseHistory h : hist) {
        if (histmap.containsKey(h.CaseId)) {
           continue;
        } else {
           histmap.put(h.CaseId, h.CreatedDate);
        }
     }

     Integer flag=0;
     String contents = '<table width=500>' +
                                    '<tr><td><b>Count</td>' +
                                    '<td><b>Queue</td>' +
                                    '<td><b>CaseNumber</td>' +
                                    '<td><b>IdleTime</td></tr>\n';

     List<Case> tkts = [select Id,CaseNumber,OwnerId from Case
                                       where RecordTypeId='01230000000z7hDAAQ'
                                       and isClosed=false];

 

     Integer lcv=1;
     for (Case a : tkts) {

        /* only want cases assigned to queues */
        if (!grpmap.containsKey(a.OwnerId) ) {
           continue;
        }

        String grpNm = grpmap.get(a.OwnerId);
        Datetime nowDate = Datetime.now();
        Datetime histDate = histmap.get(a.Id);
        Datetime idlDate = histDate.addHours(3);

        /* find the diff in hours */
        Decimal idleMin = (nowDate.getTime() - histDate.getTime())/(1000*60);
        Decimal idleHrs = idleMin.Divide(60,2);
        Decimal idleDys = idleHrs.Divide(24,2);

        String idlTm = idleMin + ' mins';
        if (idleMin > 60) { /* convert to hours */
            idlTm = idleHrs + ' hrs';
        }
        if (idleHrs > 24) { /* convert to days */
            idlTm = idleDys + ' days';
        }
       System.debug('idle time (' + a.CaseNumber + '): ' + idlTm );

       /* idle for > 3 hours */
       if (idleMin > 3) { 
           contents += '<tr><td>' + lcv + '</td>' +
                                 '<td>' + grpNm + '</td>' +
                                '<td>' + a.CaseNumber + '</td>' +
                                '<td>' + idlTm + '</td></tr>\n';
          flag=1;
          lcv++;
       }

     }

     if (flag == 1) {

         contents += '</table>';

         List<Messaging.SingleEmailMessage> mailList = new list<Messaging.SingleEmailMessage>();
         Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
   

         String[] address = new String[]{'fake.email@test.com'};
         mail.setToAddresses(address);
         mail.setSubject('IdleCases Report - SandBox');
         mail.setHtmlBody(contents);
         mailList.add(mail);

         Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
         System.debug('Email Sent: ' + results.get(0).isSuccess() );
     }


 }
}

Regular Contributor
Moggy
Posts: 27
0

Re: Code Coverage - Apex unit testing and deployment

I am still very new to all that trigger and coverage, but isn't there the limits missing Test.startTest() and Test.stopTest() ?

sorry if I am wrong

Not everything that counts can be counted,
and not everything that can be counted counts.
@Albert Einstein
Contributor
nGenxSystems
Posts: 3
0

Re: Code Coverage - Apex unit testing and deployment

I did not include any of the @isTest includes or assert statements as they made no difference in my attempts - so I obviousy am missing HOW to properly add the proper unit tests to get the 75% code coverage. I understand that I need those syntactically yes, but was hoping someone could look at my 'production' code and say - "this is basically what you need to do to move forward" ... :)

Regular Contributor
Moggy
Posts: 27
0

Re: Code Coverage - Apex unit testing and deployment

[ Edited ]

another thing I have read about the test is that your test is within a "testMethod" and that isn't in your class , maybe a try to write a single test method which then calls the 

 SendReport() method.

 

or did you tried that already

 

 

see also about the testing at http://wiki.developerforce.com/page/An_Introduction_to_Apex_Code_Test_Methods

 

 

so try to add before the last }

 

static testMethod void sendingMyReport(){

           test.start.Test();

           this.SendReport();

           test.stop.Test();

}

 

and let me know

Not everything that counts can be counted,
and not everything that can be counted counts.
@Albert Einstein
Super Contributor
Vinit_Kumar
Posts: 645
0

Re: Code Coverage - Apex unit testing and deployment

Hi,

 

I don't see a testmetod written for your controller class.Please write a testmethod and then you can get the code coverage.

 

Please go through the below link to see how to write a testmetod for controllers:-

 

http://www.salesforce.com/us/developer/docs/pages/Content/pages_controller_error_handling.htm

Don't forget to give KUDOS if post helped you.

If this answers your query,please mark this as solution so that it would be useful for others.


Contributor
nGenxSystems
Posts: 3
0

Re: Code Coverage - Apex unit testing and deployment

Let me add my test code that I've already written - I did not include it here b/c it was passing/compiling but with 0% ... give me a few hous. Thanks.