You need to sign in to do that
Don't have an account?
Andrejs Antonovs
Apex Class Code Coverage does not change (0% and 0 lines)
Hello.
How ar eyou doing today?
Maybe you can try to help me with one issue. I have Apex Class - EmployeeMonthlyEmail, which takes collection of employees, and sends them emails. And another Test Apex Class - TestEmployeeMonthlyEmail for code coverage. Unfortunately, code coverage is 0%, and I am unable to understand why.
Please see code below. Based on test run results, situations with Good and Bad emails provided to a function are covered, and tests finish correctly returning True/False as expected. However, Code Coverage is at 0% and 0/27 lines.
Here is a screen shot with code coverage info and test run:
http://screencast.com/t/tWsfyHud
Please note that during debug execution, code works as expected and emails are received on GMail account. It means code is written corretly, but Code Coverage probably is done inappropriately.
Any ideas what is wrong in this situation?
p.s. TestDataFactory class works as expected - generates records, previously test code did not use DataFactory and code coverage was also 0%
------------ Apex Class EmployeeMonthlyEmail
public class EmployeeMonthlyEmail {
public static Boolean sendEmailsToEmployees() {
List<Employee__c> empList =
[SELECT Name, FirstName__c, LastName__c, Email__c
FROM Employee__c
WHERE Status__c != 'Disabled'
];
for(Employee__c emp:empList){
sendEmailToEmployee(emp);
}
return true;
}
public static Boolean sendEmailToEmployee(Employee__c emp){
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setToAddresses(new String[]{emp.Email__c});
mail.setReplyTo('name.surname@gmail.com');
mail.setSenderDisplayName('Salesforce');
mail.setSubject('Information about ' + emp.FirstName__c + ' ' + emp.LastName__c);
mail.setBccSender(false);
mail.setUseSignature(false);
mail.setCharset('UTF-8');
mail.setHtmlBody(
'<p>Hello ' + emp.Name +',</p>'+
'<p>Please check information we have, and let us know if we should update it.'+
'<br />Thank you in advance ;)</p>'+
'<p>First Name: '+ emp.FirstName__c + '<br />'+
'Last Name: '+ emp.LastName__c + '</p>');
try {
List<Messaging.SendEmailResult> results =
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
if (!results.get(0).isSuccess()) {
return false;
} else {
return true;
}
} catch (Exception e) {
System.debug('Exception has occurred: ' + e.getMessage());
return false;
}
}
}
------------ Test Apex Class EmployeeMonthlyEmail
@isTest
public class TestEmployeeMonthlyEmail {
@isTest static void testSendEmailToEmployeeSuccess(){
List<Employee__c> empList = TestDataFactory.createEmployees();
Boolean result = EmployeeMonthlyEmail.sendEmailToEmployee(empList[0]);
System.assertEquals(true,result);
}
@isTest static void testSendEmailToEmployeeFail(){
List<Employee__c> empList = TestDataFactory.createEmployees();
empList[0].Email__c = 'bademailname.com';
Boolean result = EmployeeMonthlyEmail.sendEmailToEmployee(empList[0]);
System.assertEquals(false,result);
}
@isTest static void testSendEmailsToEmployees(){
List<Employee__c> empList = TestDataFactory.createEmployees();
Boolean result = EmployeeMonthlyEmail.sendEmailsToEmployees();
System.assertEquals(true,result);
}
}
How ar eyou doing today?
Maybe you can try to help me with one issue. I have Apex Class - EmployeeMonthlyEmail, which takes collection of employees, and sends them emails. And another Test Apex Class - TestEmployeeMonthlyEmail for code coverage. Unfortunately, code coverage is 0%, and I am unable to understand why.
Please see code below. Based on test run results, situations with Good and Bad emails provided to a function are covered, and tests finish correctly returning True/False as expected. However, Code Coverage is at 0% and 0/27 lines.
Here is a screen shot with code coverage info and test run:
http://screencast.com/t/tWsfyHud
Please note that during debug execution, code works as expected and emails are received on GMail account. It means code is written corretly, but Code Coverage probably is done inappropriately.
Any ideas what is wrong in this situation?
p.s. TestDataFactory class works as expected - generates records, previously test code did not use DataFactory and code coverage was also 0%
------------ Apex Class EmployeeMonthlyEmail
public class EmployeeMonthlyEmail {
public static Boolean sendEmailsToEmployees() {
List<Employee__c> empList =
[SELECT Name, FirstName__c, LastName__c, Email__c
FROM Employee__c
WHERE Status__c != 'Disabled'
];
for(Employee__c emp:empList){
sendEmailToEmployee(emp);
}
return true;
}
public static Boolean sendEmailToEmployee(Employee__c emp){
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setToAddresses(new String[]{emp.Email__c});
mail.setReplyTo('name.surname@gmail.com');
mail.setSenderDisplayName('Salesforce');
mail.setSubject('Information about ' + emp.FirstName__c + ' ' + emp.LastName__c);
mail.setBccSender(false);
mail.setUseSignature(false);
mail.setCharset('UTF-8');
mail.setHtmlBody(
'<p>Hello ' + emp.Name +',</p>'+
'<p>Please check information we have, and let us know if we should update it.'+
'<br />Thank you in advance ;)</p>'+
'<p>First Name: '+ emp.FirstName__c + '<br />'+
'Last Name: '+ emp.LastName__c + '</p>');
try {
List<Messaging.SendEmailResult> results =
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
if (!results.get(0).isSuccess()) {
return false;
} else {
return true;
}
} catch (Exception e) {
System.debug('Exception has occurred: ' + e.getMessage());
return false;
}
}
}
------------ Test Apex Class EmployeeMonthlyEmail
@isTest
public class TestEmployeeMonthlyEmail {
@isTest static void testSendEmailToEmployeeSuccess(){
List<Employee__c> empList = TestDataFactory.createEmployees();
Boolean result = EmployeeMonthlyEmail.sendEmailToEmployee(empList[0]);
System.assertEquals(true,result);
}
@isTest static void testSendEmailToEmployeeFail(){
List<Employee__c> empList = TestDataFactory.createEmployees();
empList[0].Email__c = 'bademailname.com';
Boolean result = EmployeeMonthlyEmail.sendEmailToEmployee(empList[0]);
System.assertEquals(false,result);
}
@isTest static void testSendEmailsToEmployees(){
List<Employee__c> empList = TestDataFactory.createEmployees();
Boolean result = EmployeeMonthlyEmail.sendEmailsToEmployees();
System.assertEquals(true,result);
}
}
Just add Test.startTest(), Test.stopTest(); and your test class will run
Good luck !!!
Yonathan G.
Thank you for the quick reply.
Unfortunately, it did not help. After adding start/stop lines, and running tests, code coverage did not change, and remains 0%.
Any other ideas?
I added 1 more method to the class, and added test method for it.
After Run All Tests, I get 97% Code Coverage.
But, if I remove unwanted method, I get 0% Code Coverage.
So very strange, and not sure what is the best way to continue.
Try to remove try/catch and check your log to see what is the error.
You can send here the log for more help
;)
Yonathan
I did not check your solution, but this workaround worked for me:
- create dull method
- create test coverage for dull method
public static Boolean CodeCoverageFix() {
return true;
}
@isTest static void testCodeCoverageFix() {
Boolean result = EmployeeMonthlyEmail.CodeCoverageFix();
System.assertEquals(true, result);
}