You need to sign in to do that
Don't have an account?

Test class for schedulable job with SOQL and method call
Hi,
I am not not able to write test class with more than 75% coverage. Please help me with test class for below job -
global class CreateProjectTaskBatchJob implements Schedulable{
global void execute(SchedulableContext SC) {
List<String> orderIds = New List<String>();
List<Order> orders = [select Id from order where Type='Asset' and Close_Won_Date__c >= TODAY and Order_Location__c != NULL];
for(Order o : orders) {
orderIds.add(String.valueOf(o.id));
}
if(orderIds.size() > 0) {
CreateProjectTask.createTasks(orderIds);
}
}
}
I am not not able to write test class with more than 75% coverage. Please help me with test class for below job -
global class CreateProjectTaskBatchJob implements Schedulable{
global void execute(SchedulableContext SC) {
List<String> orderIds = New List<String>();
List<Order> orders = [select Id from order where Type='Asset' and Close_Won_Date__c >= TODAY and Order_Location__c != NULL];
for(Order o : orders) {
orderIds.add(String.valueOf(o.id));
}
if(orderIds.size() > 0) {
CreateProjectTask.createTasks(orderIds);
}
}
}
Can you please post the test class which you have written for the same so experts can check the test class and suggest you on it.
Thanks,
@isTest
public class CreateProjectTaskBatchJobTest {
@isTest static void myTest() {
Account acc = New Account();
acc.Name = 'Test Account';
acc.RecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByDeveloperName().get(Constants.CUSTOMER_RECORDTYPE_DN).getRecordTypeId();
acc.SBQQ__TaxExempt__c = 'Yes';
INSERT acc;
Opportunity opp = New Opportunity();
opp.name = 'Test';
opp.CloseDate = System.today();
opp.stageName = 'Discovery';
INSERT opp;
Order testOrder2 = new Order();
testOrder2.Name = 'Test Order ';
testOrder2.Status = 'Draft';
testOrder2.OpportunityId = opp.id;
testOrder2.EffectiveDate = system.today();
testOrder2.EndDate = system.today() + 4;
testOrder2.AccountId = acc.Id;
testOrder2.blng__BillingDayOfMonth__c = '1';
testOrder2.Pricebook2Id = Test.getStandardPricebookId();
testOrder2.Type = 'Asset';
INSERT testOrder2;
List<String> orderIds = New List<String>();
orderIds.add(testOrder2.Id);
CreateProjectTask.createTasks(orderIds);
CreateProjectTaskBatchJob sh1 = new CreateProjectTaskBatchJob();
//SchedulableContext sc = null;
//CreateProjectTaskBatchJob sh1 = new CreateProjectTaskBatchJob();
//sh1.execute(sc);
// This test runs a scheduled job at midnight Sept. 3rd. 2022
String sch = '0 0 0 3 9 ? 2022';
String jobId = system.schedule('Test check', sch, sh1);
// Get the information from the CronTrigger API object
CronTrigger ct = [SELECT Id, CronExpression, TimesTriggered, NextFireTime FROM CronTrigger WHERE id = :jobId];
// Verify the expressions are the same System.assertEquals(CRON_EXP, ct.CronExpression);
// Verify the job has not run
System.assertEquals(0, ct.TimesTriggered);
// Verify the next time the job will run
System.assertEquals('2022-09-03 00:00:00', String.valueOf(ct.NextFireTime));
}
}
Can you try to populate the Close_Won_Date__c as system.today()+1 and Order_Location__c field in the test data for order. Because the test order is not satisfyiing these conditions it is not covering the remaining part.
Please let me know if you still face the same.
If this solution helps, Please mark it as best answer.
Thanks,
Can you check if that is a formula field and let me know what exactly is the formula so we can adjust it based on that.
Thanks,
In that scenerio while inserting opportunity you need to populate the close date as
And also give the Order_Location__c field while creating the order. This should solve the coverage issue.
Thanks,
You may need to create the Custom_Location__c record as we did for opportuity and order and share us the code coverage screenshot so can check and confirm what part is not covering.
Thanks,
As of now test class