You need to sign in to do that
Don't have an account?
trailhead solutions 10
Code Coverage not executing for Execute Method in Batch Apex
Hello
I have written a Batch Class to change published Knowledge Articles to Draft and Vice Versa as well.Class is working fine. I have successfully changed the status from online to draft, updated 36k records for 1 field with data loader, and again change to online.
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, where to change in Apex and Test Class?.
Can someone please try in their Org and let me know. Need to deploy this to Production urgent.
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.Class is working fine. I have successfully changed the status from online to draft, updated 36k records for 1 field with data loader, and again change to online.
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, where to change in Apex and Test Class?.
Can someone please try in their Org and let me know. Need to deploy this to Production urgent.
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
Thanks for sharing the Test Class.
I have also written in some similar code in my Org and it covers the Execute Part . but slight changes in it I did.
@isTest
private class TestKarepublishBatch {
@isTest
private static void publishArticle(){
Test.startTest();
String articleTitle = 'Test Article';
String articleBody = 'Test Body';
String articleUrlName = 'test-Article';
String language = 'en_US';
string PublishStatus = 'online';
string Owner='Consumer Support';
Knowledge__kav article = new Knowledge__kav(
Title = articleTitle,
Summary = articleBody,
UrlName = articleUrlName,
Language = language,
ps_CX_Functional_Owner__c=Owner
);
insert article;
Knowledge__kav ka = [SELECT ArticleCreatedDate, Publishstatus,ArticleNumber FROM Knowledge__kav WHERE Id =: article.Id and title='test article' and PublishStatus='draft' and ps_CX_Functional_Owner__c='Consumer Support'];
KarepublishBatch ka1= new KarepublishBatch();
Id batchId = Database.executeBatch(ka1);
Test.stopTest();
}
}
Thanks
GSN
All Answers
your excute method is not covering because you data is not able to come in query. so simply you have to update your inserted Knowledge__kav record.
try below test class.
don't forget to mark it as best answer.
Thank you
Thanks for sharing the Test Class.
I have also written in some similar code in my Org and it covers the Execute Part . but slight changes in it I did.
@isTest
private class TestKarepublishBatch {
@isTest
private static void publishArticle(){
Test.startTest();
String articleTitle = 'Test Article';
String articleBody = 'Test Body';
String articleUrlName = 'test-Article';
String language = 'en_US';
string PublishStatus = 'online';
string Owner='Consumer Support';
Knowledge__kav article = new Knowledge__kav(
Title = articleTitle,
Summary = articleBody,
UrlName = articleUrlName,
Language = language,
ps_CX_Functional_Owner__c=Owner
);
insert article;
Knowledge__kav ka = [SELECT ArticleCreatedDate, Publishstatus,ArticleNumber FROM Knowledge__kav WHERE Id =: article.Id and title='test article' and PublishStatus='draft' and ps_CX_Functional_Owner__c='Consumer Support'];
KarepublishBatch ka1= new KarepublishBatch();
Id batchId = Database.executeBatch(ka1);
Test.stopTest();
}
}
Thanks
GSN