function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
AngelikaAngelika 

Code Coverage Help: How to get more (workarounds) please help

I have written a test method for an Apex Birthday Scheduler Class. The class works great in Sandbox, sending an email to the CEO 2 days before an employees birthday.

 

However, I can't transfer this class to production because my class has 0 test coverage despite writting a test method that creates an account and contact that satisfies my class's query. Am I missing something here? Is there a way to "cheat" and get coverage?

 

I've spent hours on this to no avail. 

 

Here is my code and test method (red indicates lines not covered in Salesforce:

 

global class Birthday implements Schedulable{
global void execute (SchedulableContext ctx)
{

sendBirthdayEmail();

}
public void sendBirthdayEmail()
{

for(Contact con : [SELECT name, Id, Birthdate FROM Contact WHERE Next_Birthday__c = : system.Today().addDays(2) AND Account.Name = 'Company Inc.'])

{
String conId = con.Id;
String conName = con.name;
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
//mail.setTemplateId('00XJ0000000M31w');

mail.setTargetObjectId('005J0000000JWYx');
// mail.setToAddresses('email@company.com');
mail.setsubject('Company Birthday Reminder');
mail.setHtmlBody('This is a scheduler-generated email to notify you that the birthday of Name: <b> ' + con.name + ' </b> is in two days. Birthdate of: ' + con.Birthdate + '. Please wish them a happy Birthday.');
mail.setSaveAsActivity(false);
Messaging.sendEmail(new Messaging.SingleEmailMessage[] {mail });
}

}

 

// test method

static testMethod void myTestBirthday() {

//create the required test data needed for the test scenario
//In this case, I need to create a new contact, with the account name of Company and has a birthday 2 days away
Account testAccount = new Account(name='Company Inc.');
insert testAccount;
Contact testContact = new Contact();
testContact.firstName='Jack';
testContact.lastName='Dell';
insert testContact;

testContact.birthdate=system.Today().addDays(2);

update testContact;

testAccount.name = testContact.id;

//verify that the birthdate field was updated in the database

//Contact updatedContact = [SELECT birthdate FROM Contact WHERE Id =:testContact.Id];
//System.assertEquals(system.Today().addDays(2) , updatedContact.birthdate);

 

//Contact nextUpdatedContact = [SELECT Account.name FROM Contact WHERE Id =:testContact.Id];
//System.assertEquals(system.Today().addDays(2) , nextupdatedContact.birthdate);

for(Contact con : [SELECT name, Id, Birthdate FROM Contact WHERE Next_Birthday__c = : system.Today().addDays(2) AND Account.Name = 'Company Inc.'])
{
String conId = con.Id;
String conName = con.name;
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
//mail.setTemplateId('00XJ0000000M31w');

mail.setTargetObjectId('005J0000000JWYx');
// mail.setToAddresses('company@company.com');
mail.setsubject('Company Birthday Reminder');
mail.setHtmlBody('This is a scheduler-generated email to notify you that the birthday of Name: <b> ' + con.name + ' </b> is in two days. Birthdate of: ' + con.Birthdate + '. Please wish them a happy Birthday.');
mail.setSaveAsActivity(false);
Messaging.sendEmail(new Messaging.SingleEmailMessage[] {mail });
}
}

 

}

ColinMcGColinMcG

Unless I'm missing the point somewhere, you haven't actually called any of your code from the test method. I think all you need to do is call the sendBirthdayEmail method after setting up your contact.

AngelikaAngelika

How would I do this? I'm super new to apex? Thanks for your help!

ColinMcGColinMcG

I can't look deeply into this at the moment, but I think that what you need to do is get rid of the code on the test method which repeats what's already part of your original code. To test your code, you need to actually call it from your test method. So, in your case, your first bit seems ok (certainly, the concept is correct, though I can't guarantee your logic is perfect without looking closer). After your test code sets up the contact data, you just need a line like this:

 

Birthday.sendBirthdayEmail();

 

That then calls your code and gives you coverage of every line which runs as a result.

ColinMcGColinMcG

.. and looking a little more at the setup of your contact, you need to attach your contact to the account, otherwise zero rows will be returned. i.e. set the Account ID on the Contact to the ID of the Account just created.

Anoop AsokAnoop Asok

Hey,

 

Try this.

 

// test method

static testMethod void myTestBirthday() {

//create the required test data needed for the test scenario
//In this case, I need to create a new contact, with the account name of Company and has a birthday 2 days away
Account testAccount = new Account(name='Company Inc.');
insert testAccount;
Contact testContact = new Contact();
testContact.firstName='Jack';
testContact.lastName='Dell';
insert testContact;

testContact.birthdate=system.Today().addDays(2);
testContact.AccountId = testAccount.id; //[Anoop] Linking contact and account records.
update testContact;

//testAccount.name = testContact.id; [Anoop] Commented.

//verify that the birthdate field was updated in the database

//Contact updatedContact = [SELECT birthdate FROM Contact WHERE Id =:testContact.Id];
//System.assertEquals(system.Today().addDays(2) , updatedContact.birthdate);

 

//Contact nextUpdatedContact = [SELECT Account.name FROM Contact WHERE Id =:testContact.Id];
//System.assertEquals(system.Today().addDays(2) , nextupdatedContact.birthdate);
/** [Anoop] You need to invoke the below logic from your class method rather than writing it here.
    Hence commenting out... 
for(Contact con : [SELECT name, Id, Birthdate FROM Contact WHERE Next_Birthday__c = : system.Today().addDays(2) AND Account.Name = 'Company Inc.'])
{
String conId = con.Id;
String conName = con.name;
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
//mail.setTemplateId('00XJ0000000M31w');

mail.setTargetObjectId('005J0000000JWYx');
// mail.setToAddresses('company@company.com');
mail.setsubject('Company Birthday Reminder');
mail.setHtmlBody('This is a scheduler-generated email to notify you that the birthday of Name: <b> ' + con.name + ' </b> is in two days. Birthdate of: ' + con.Birthdate + '. Please wish them a happy Birthday.');
mail.setSaveAsActivity(false);
Messaging.sendEmail(new Messaging.SingleEmailMessage[] {mail });
}
.. **/
Birthday.sendBirthdayEmail(); // [Anoop] Invoking class method.
}

 

I've marked the changes with [Anoop].

 

Thanks,

Anoop