You need to sign in to do that
Don't have an account?
sandy k
How to get BatchableContext Jobid in test method
Hi Guys,
I am writing test class and my test code fails to cover below highlighted code and which inturn does not include execute method in the coverage. Let me know how to include code to fulfill below condition
public Database.QueryLocator start(Database.BatchableContext BC) {
Integer limitInt = 10000;
List<AsyncApexJob> aajList = new List<AsyncApexJob>();
if(lastJobId!=null && lastJobId!=BC.getJobId()) {
aajList.addAll([SELECT Id, Status, CompletedDate FROM AsyncApexJob WHERE ID =: lastJobId and JobType!='BatchApexWorker' and Status not in ('Aborted','Completed','Failed')]);
if(lastJobId==null || aajList.isEmpty()) {
lastJobId = BC.getJobId();
the if condition in the above code is failing.
I am writing test class and my test code fails to cover below highlighted code and which inturn does not include execute method in the coverage. Let me know how to include code to fulfill below condition
public Database.QueryLocator start(Database.BatchableContext BC) {
Integer limitInt = 10000;
List<AsyncApexJob> aajList = new List<AsyncApexJob>();
if(lastJobId!=null && lastJobId!=BC.getJobId()) {
aajList.addAll([SELECT Id, Status, CompletedDate FROM AsyncApexJob WHERE ID =: lastJobId and JobType!='BatchApexWorker' and Status not in ('Aborted','Completed','Failed')]);
if(lastJobId==null || aajList.isEmpty()) {
lastJobId = BC.getJobId();
the if condition in the above code is failing.
What is the variable lastJobId. I dont see the declaration and assignment for that variable. It would be hard for experts to answer without that information.
Thanks,
You need to make sure what lastJobId is coming due to test class execution, so use below line and let me know what is your output
system.debug('lastJobId==>> '+lastJobId);
if you need any assistanse, Please let me know!!
Kindly mark my solution as the best answer if it helps you.
Thanks
Mukesh
lastJobId is Id type variable and initialised with null