Discussions
- General Development
- Schema Development
- Apex Code Development
- Visualforce Development
- Formulas & Validation Rules
- Security
- Mobile
- Force.com Sites & Site.com
- Chatter Development
- Java Development
- .NET Development
- Perl, PHP, Python & Ruby
- Desktop Integration
- APIs and Integrations
- Visual Workflow
- Apple, Mac and OS X
- VB and Office Development
- AppExchange Directory & Packaging
- Salesforce Labs & Open Source Projects
- Other Salesforce Applications
- Jobs Board
- Force.com Discussion Boards
- :
- Developer Boards for Force.com and Database.com
- :
- Apex Code Development
- :
- Code Coverage - Apex unit testing and deployment
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic to the Top
- Bookmark
- Subscribe
- Printer Friendly Page
Code Coverage - Apex unit testing and deployment
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
01-28-2013 02:32 PM
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() );
}
}
}
Re: Code Coverage - Apex unit testing and deployment
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
01-28-2013 02:47 PM
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
and not everything that can be counted counts.
@Albert Einstein
Re: Code Coverage - Apex unit testing and deployment
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
01-28-2013 02:50 PM
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" ... :)
Re: Code Coverage - Apex unit testing and deployment
[ Edited ]- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
01-28-2013 03:13 PM - edited 01-28-2013 11:00 PM
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_Introductio
so try to add before the last }
static testMethod void sendingMyReport(){
test.start.Test();
this.SendReport();
test.stop.Test();
}
and let me know
and not everything that can be counted counts.
@Albert Einstein
Re: Code Coverage - Apex unit testing and deployment
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
01-29-2013 02:10 AM
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/
If this answers your query,please mark this as solution so that it would be useful for others.
Re: Code Coverage - Apex unit testing and deployment
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
01-29-2013 10:29 AM
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.

