You need to sign in to do that
Don't have an account?
trailhead 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
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
Try Below Code Please Mark It As Best Answer If It Helps
Thank You!
Can someone please help on this, as I have to deploy the class Immediately.
Thanks
GSN
In Start Method Print The Qurey In System Debug And See Does it Returns Records Or Not