function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
trailhead solutions 10trailhead solutions 10 

Increase Code Coverage for my simple Batch Apex

Hello

I have written a Batch Class to change published Knowledge Articles to Draft and Vice Versa as well. But the Test class is Passes with 44% code coverage. Start and Finish are fine. But, Not running the execute method. Not sure, where I did wrong. Can someone please help me in this.

Batch Class:
global class KarepublishBatch implements Database.Batchable<sObject> {
    
    global Database.QueryLocator start(Database.BatchableContext BC){
    String query = 'SELECT Id,title, ArticleNumber,ps_CX_Functional_Owner__c, publishstatus,KnowledgeArticleId FROM Knowledge__kav WHERE PublishStatus = \'draft\' and ps_CX_Functional_Owner__c!=null';
        return Database.getQueryLocator(query);
    }


global void execute(Database.BatchableContext BC, list<Knowledge__kav> Draftlist){
    for(Knowledge__kav k :Draftlist){
    
    try{
KbManagement.PublishingService.publishArticle(k.knowledgearticleId, true);  
    }
    catch(exception e){
        system.debug('record failed:'+e.getmessage());
    }

}
global void finish(Database.BatchableContext BC) {

    }
}

Test Class:
=========
@isTest
public class TestKarepublishBatch {
static testmethod void publishArticle(){
    String articleTitle = 'Test Article';
    String articleBody = 'Test Body';
    String articleUrlName = 'test-Article';
    String language = 'en_US';
    string PublishStatus = 'online';

    Knowledge__kav article = new Knowledge__kav(
      Title = articleTitle,
      Summary = articleBody,
      UrlName = articleUrlName,
      Language = language
      //PublishStatus = PublishStatus  
    );

    insert article; 
    Knowledge__kav ka = [SELECT ArticleCreatedDate, Publishstatus,ArticleNumber FROM Knowledge__kav WHERE Id =: article.Id and title='test article'];
    
    String articleId = ka.Id;
 try{
KbManagement.PublishingService.publishArticle(articleId, true);  
    }
    catch(exception e){
        system.debug('record failed:'+e.getmessage());
    }
    Test.startTest();
    KarepublishBatch ka1= new KarepublishBatch();
        Id batchId = Database.executeBatch(ka1,1);
        Test.stopTest();
}
}



Thanks
GSN
CharuDuttCharuDutt
Hiii
Try Below Code
@isTest
public class TestKarepublishBatch {
@isTest
public static void UniTest(){

Knowledge__kav  k = new Knowledge__kav ();
/*Fill All Fields Required*/
insert k;

        test.startTest();
        KarepublishBatch ka1= new KarepublishBatch();
        Id batchId = Database.executeBatch(ka1);
        test.stopTest();
}
}
Please Mark It As Best Answer If It Helps
Thank You!
trailhead solutions 10trailhead solutions 10
Hi  @CharuDutt: Made few changes, but still code coverage is same 44%.
Can someone please help on this, as I have to deploy the class Immediately.

Thanks
GSN
CharuDuttCharuDutt
Hii 
In Start Method Print The Qurey In System Debug And See Does it Returns Records Or Not