I have a batch test class:
@isTest
public with sharing class TestBatchManagement {
@isTest
public static void batchManagementTest() {
User u = TestDataFactory.createUserWithRoleLeader();
Test.startTest();
System.runAs(u) {
List<Knowledge__kav> k = TestDataFactory.createKnowledgeExternalFAQ();
Knowledge__kav kQuery = [
SELECT id, createdByID, createdBy.UserRole.DeveloperName,LastModifiedBy.UserRole.DeveloperName,
RecordType.DeveloperName
FROM Knowledge__kav
WHERE LastModifiedBy.UserRole.DeveloperName != 'Super'
AND ((LastModifiedBy.UserRole.DeveloperName= 'Leader' AND RecordType.DeveloperName != 'CC') OR LastModifiedBy.UserRole.DeveloperName != 'Leader')
];
}
BatchManagement bm = new BatchManagement();
Database.executeBatch(bm);
Test.stopTest();
}
}
The problem is in the batch class the query locator returns no records. Even in the test class, if I do a query for Knowledge__kav, it returns List has no rows for assignment to SObject
unless in the WHERE
I add this id =: k[0].id
.
Why is it so? And how can I solve this?
UPDATE:
doing KbManagement.PublishingService.publishArticle(kQuery.KnowledgeArticleId, false);
in the test class did the trick. In the batch class, the start method returns the query and the inserted KAV's PublishStatus
is Online
.
But unfortunately in the batch class I have condition where the KAV's PublishStatus
has to Draft
or Archived
. So now I can return the KAV I created in test class through the batch's start method but because of the condition the code coverage is insufficient.
Database.executeBatch()
; Option 2: Use dependency injection to the batch class's object to provide an alternate selector for the start() method