I have a batch test class:

public with sharing class TestBatchManagement {
    public static void batchManagementTest() {
        User u = TestDataFactory.createUserWithRoleLeader();
        System.runAs(u) {
            List<Knowledge__kav> k = TestDataFactory.createKnowledgeExternalFAQ();
            Knowledge__kav kQuery = [
                SELECT id, createdByID, createdBy.UserRole.DeveloperName,LastModifiedBy.UserRole.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();

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?


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.

  • How would a query in the test class return null? It should either return an exception or an empty list if no records are found. Assuming that is the actual behaviour, does the query return no records even if you completely omit the WHERE clause? Dec 11, 2020 at 11:24
  • by null I meant "List has no rows for assignment to SObject" yes if I omit the where clause it says "List has no rows for assignment to SObject".
    – nSv23
    Dec 11, 2020 at 11:29
  • Option 1: test the batch class start(), execute(), and finish() methods separately rather than using Database.executeBatch(); Option 2: Use dependency injection to the batch class's object to provide an alternate selector for the start() method
    – cropredy
    Dec 12, 2020 at 5:20

1 Answer 1


It seems the problem is with the Apex API version. The API version was 46 when the project started. Changing it version 48.0 worked.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .