You need to sign in to do that
Don't have an account?
Matt Metros
My test data is not appearing
@isTest
private class NumberOfEmailsSchedulableTest {
public static final Integer numberOfAcc = 5;
public static final Integer numberOfCon = 2;
static testmethod void schedulerTest()
{
CreateTestAccountsContactsAndTasks(numberOfAcc, numberOfCon);
List<Task> tasks = [SELECT id, subject, ActivityDate, Sequence_Name__c FROM Task];
String CRON_EXP = '0 0 0 15 3 ? *';
// Create your test data
Account acc = new Account();
acc.name= 'test';
insert
acc;
Test.startTest();
String jobId = System.schedule('ScheduleApexClassTest', CRON_EXP, new NumberOfEmailsSchedulable());
CronTrigger ct = [SELECT Id, CronExpression, TimesTriggered, NextFireTime FROM CronTrigger WHERE id = :jobId];
System.assertEquals(CRON_EXP, ct.CronExpression);
System.assertEquals(0, ct.TimesTriggered);
Test.stopTest();
// Add assert here to validate result
}
public static void CreateTestAccountsContactsAndTasks(Integer numOfAccounts, Integer contactsPerAccount)
{
Database.DMLOptions dml = new Database.DMLOptions();
dml.DuplicateRuleHeader.allowSave = true;
List<Account> accounts = new List<Account>();
for(Integer i = 0; i < numOfAccounts; i++)
{
// create new account
Account newAccount = new Account(
Name = 'Test' + String.valueOf(i),
Industry = 'Accounting',
of_Locations__c = 5 + i,
sdr_owner__c = userinfo.getuserid()
);
accounts.add(newAccount);
}
Database.insert(accounts, dml);
List<Contact> contacts = new List<Contact>();
for(Account act: accounts){
for(Integer i = 0; i < contactsPerAccount; i++)
{
// create new contact
Contact con = new Contact(
LastName = 'Test',
AccountId = act.id,
Outreach_Status__c = 'In Contact',
Title = 'Test' + String.valueOf(i),
Email = 'test@test.com'
);
contacts.add(con);
}
}
Database.insert(contacts, dml);
List<Task> tasksToInsert = new List<Task>();
for(Contact con : contacts)
{
Task regularTask = new Task(
whoid = con.Id,
ActivityDate = Date.today(),
Status = 'Completed'
);
Task personalizedTask = new Task(
Sequence_Name__c = 'personalized',
whoid = con.Id,
ActivityDate = Date.today(),
Status = 'Completed'
);
Task sequencedTask = new Task(
Sequence_Name__c = 'WYWYN19',
whoid = con.Id,
ActivityDate = Date.today(),
Status = 'Completed'
);
tasksToInsert.add(regularTask);
tasksToInsert.add(personalizedTask);
tasksToInsert.add(sequencedTask);
}
Database.insert(tasksToInsert, dml);
}
public static User createUser(String username , String theAlias)
{
Integer randomInt = Integer.valueOf(math.rint(math.random()*1000));
User u = new User
(
Alias = theAlias + String.valueOf(randomInt),
Email = username + String.valueOf(randomInt)+ '@test.com',
FirstName = 'Joe',
LastName = username + String.valueOf(randomInt),
TimeZoneSidKey = 'America/Los_Angeles',
UserName = username + String.valueOf(randomInt) + '@test.com',
LocaleSidKey = 'en_US',
EmailEncodingKey = 'UTF-8',
LanguageLocaleKey = 'en_US',
ProfileID = UserInfo.getProfileId()
);
insert u;
return u;
}
}
I user database.Insert and no tasks appear. Why?
private class NumberOfEmailsSchedulableTest {
public static final Integer numberOfAcc = 5;
public static final Integer numberOfCon = 2;
static testmethod void schedulerTest()
{
CreateTestAccountsContactsAndTasks(numberOfAcc, numberOfCon);
List<Task> tasks = [SELECT id, subject, ActivityDate, Sequence_Name__c FROM Task];
String CRON_EXP = '0 0 0 15 3 ? *';
// Create your test data
Account acc = new Account();
acc.name= 'test';
insert
acc;
Test.startTest();
String jobId = System.schedule('ScheduleApexClassTest', CRON_EXP, new NumberOfEmailsSchedulable());
CronTrigger ct = [SELECT Id, CronExpression, TimesTriggered, NextFireTime FROM CronTrigger WHERE id = :jobId];
System.assertEquals(CRON_EXP, ct.CronExpression);
System.assertEquals(0, ct.TimesTriggered);
Test.stopTest();
// Add assert here to validate result
}
public static void CreateTestAccountsContactsAndTasks(Integer numOfAccounts, Integer contactsPerAccount)
{
Database.DMLOptions dml = new Database.DMLOptions();
dml.DuplicateRuleHeader.allowSave = true;
List<Account> accounts = new List<Account>();
for(Integer i = 0; i < numOfAccounts; i++)
{
// create new account
Account newAccount = new Account(
Name = 'Test' + String.valueOf(i),
Industry = 'Accounting',
of_Locations__c = 5 + i,
sdr_owner__c = userinfo.getuserid()
);
accounts.add(newAccount);
}
Database.insert(accounts, dml);
List<Contact> contacts = new List<Contact>();
for(Account act: accounts){
for(Integer i = 0; i < contactsPerAccount; i++)
{
// create new contact
Contact con = new Contact(
LastName = 'Test',
AccountId = act.id,
Outreach_Status__c = 'In Contact',
Title = 'Test' + String.valueOf(i),
Email = 'test@test.com'
);
contacts.add(con);
}
}
Database.insert(contacts, dml);
List<Task> tasksToInsert = new List<Task>();
for(Contact con : contacts)
{
Task regularTask = new Task(
whoid = con.Id,
ActivityDate = Date.today(),
Status = 'Completed'
);
Task personalizedTask = new Task(
Sequence_Name__c = 'personalized',
whoid = con.Id,
ActivityDate = Date.today(),
Status = 'Completed'
);
Task sequencedTask = new Task(
Sequence_Name__c = 'WYWYN19',
whoid = con.Id,
ActivityDate = Date.today(),
Status = 'Completed'
);
tasksToInsert.add(regularTask);
tasksToInsert.add(personalizedTask);
tasksToInsert.add(sequencedTask);
}
Database.insert(tasksToInsert, dml);
}
public static User createUser(String username , String theAlias)
{
Integer randomInt = Integer.valueOf(math.rint(math.random()*1000));
User u = new User
(
Alias = theAlias + String.valueOf(randomInt),
Email = username + String.valueOf(randomInt)+ '@test.com',
FirstName = 'Joe',
LastName = username + String.valueOf(randomInt),
TimeZoneSidKey = 'America/Los_Angeles',
UserName = username + String.valueOf(randomInt) + '@test.com',
LocaleSidKey = 'en_US',
EmailEncodingKey = 'UTF-8',
LanguageLocaleKey = 'en_US',
ProfileID = UserInfo.getProfileId()
);
insert u;
return u;
}
}
I user database.Insert and no tasks appear. Why?
You are missing to add
@testSetup static void methodName() {
}
for the method which is being used for creating the test data
You can refer the following link
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing_testsetup_using.htm