You need to sign in to do that
Don't have an account?
Russell baker 1
Batch apex test code covrage only 10% in production
Hi Experts,
I have a batch class to send email reminders to task owners if their task due date are overdue with all task links. I have wrote test class and run test class for code covrage.
Code covrage 80% in sandbox but when I deployed it in production and check code covtage it is showing only 10%. Please help in this issue.
Below is my code.
And Test class is:
Please let me know why code covrage on 10%.
I have a batch class to send email reminders to task owners if their task due date are overdue with all task links. I have wrote test class and run test class for code covrage.
Code covrage 80% in sandbox but when I deployed it in production and check code covtage it is showing only 10%. Please help in this issue.
Below is my code.
global class SendEmailToDueDateTask implements Database.Batchable<sObject> { map<string,list<task>> userEmailTasklistmap = new map<string,list<task>>(); global Database.QueryLocator start(Database.BatchableContext BC){ return Database.getQueryLocator([SELECT ID,createddate,what.id,Owner.Email,OwnerId,owner.name,Status,ActivityDate,Subject from Task WHERE Status != 'Completed' and owner.isactive = true and ActivityDate =: system.today() ]); } global void execute(Database.BatchableContext BC, List<Task> scope){ for(Task Tsk : scope){ if(!userEmailTasklistmap.Containskey(tsk.owner.email)){ userEmailTasklistmap.put(tsk.owner.email, new list<task>()); } userEmailTasklistmap.get(tsk.owner.email).add(tsk); } List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>(); for(string email : userEmailTasklistmap.keyset()){ Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage(); list<string> toAddresses = new list<string>(); toAddresses.add(email); mail.setToAddresses(toAddresses); mail.setSubject('Details of tasks due for today'); String username = userEmailTasklistmap.get(email)[0].owner.name; String htmlBody = ''; htmlBody = '<table width="100%" border="0" cellspacing="0" cellpadding="8" align="center" bgcolor="#F7F7F7">'+ +'<tr>'+ +'<td style="font-size: 14px; font-weight: normal; font-family:Calibri;line-height: 18px; color: #333;"><br />'+ +'<br />'+ +'Dear '+username+',</td>'+ +'</tr>'+ +'<tr>'+ +'<td style="font-size: 14px; font-weight: normal; font-family:Calibri; line-height: 18px; color: #333;">You have Pending Task</td>'+ +'</tr>'+ +'</table>'; htmlBody += '<table border="1" style="border-collapse: collapse"><tr><th>Related To</th><th>Subject</th><th>Created Date</th><th> Due Date</th></tr>'; for(task tsk : userEmailTasklistmap.get(email)){ String duedate = ''; if (tsk.ActivityDate != null) duedate = tsk.ActivityDate.format(); else duedate = ''; String Subject = tsk.subject; datetime dt = tsk.createddate; string createddate = dt.format('M/d/yyyy'); string what = tsk.what.id; string link = URL.getSalesforceBaseUrl().toExternalForm()+'/'+ tsk.id; htmlBody += '<tr><td>' + what + '</td><td>' + Subject + '</td><td>' + createddate + '</td><td>' + duedate + '</td></tr>'; } htmlBody += '</table><br>'; mail.sethtmlBody(htmlBody); mails.add(mail); } if(mails.size()>0) Messaging.sendEmail(mails); } global void finish(Database.BatchableContext BC){ } }
And Test class is:
@isTest public class SendEmailToDueDateTaskTest { static testMethod void testMethod1(){ Profile pro = [SELECT Id FROM Profile WHERE Name='Standard User']; User usr = new User(Alias = 'standt', Email='standarduser@tt.com', EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US', LocaleSidKey='en_US', ProfileId = pro.Id, TimeZoneSidKey='America/Los_Angeles', UserName='standarduser@tt.com'); System.runAs(usr) { Account acc = new Account(); acc.Name = 'Test Account'; insert acc; acc=[SELECT id,Name FROM account WHERE id=:acc.Id]; System.assertEquals(acc.Name,'Test Account'); Task tsk = new Task(); tsk.whatId = acc.Id; tsk.Subject = 'Testing'; tsk.Status = 'In Progress'; tsk.Priority = 'Normal'; tsk.ActivityDate = System.today(); insert tsk ; tsk=[SELECT id,Status FROM Task WHERE id=:tsk.Id]; System.assertEquals(tsk.Status ,'In Progress'); } Test.StartTest(); Database.executeBatch (new SendEmailToDueDateTask (),200); Test.StopTest(); } }
Please let me know why code covrage on 10%.
Please try below test class
You Can also try below test class without creating the user
Let us know if this will help you
Thanks
Amit chaudhary