0

I have batch class to update all 'archived' articles, however when I am covering the batch class from my respective test class test, It is not showing any coverage.Please help

Batch Class

global with sharing class basic_articletype_batch implements Database.Batchable <sObject>{

       global Database.QueryLocator start(Database.BatchableContext BC) {

        return Database.getQueryLocator([SELECT Id, ArticleNumber, KnowledgeArticleId,ValidationStatus
                        FROM Basic_Technical_Q_A__kav
                        WHERE  PublishStatus = 'Archived' AND Language = 'en_US' ]);

    }

    global void execute(Database.BatchableContext BC, List<sObject> supportArticles){

       List<Id> articleIDs = new List<Id>();

        List<String> articleNumbers = new List<String>();



       //Step 1: Open published articles in editing mode which is Draft status

       for(Basic_Technical_Q_A__kav a: (List<Basic_Technical_Q_A__kav>)supportArticles){
            try{

       String id =KbManagement.PublishingService.editArchivedArticle(a.KnowledgeArticleId);

            if (Id == null) {

           System.debug('##### ERROR While Editing');

            }

            else{

            articleNumbers.add(a.ArticleNumber);

            }



       }

       catch(exception e){
       }
       }



       List<Basic_Technical_Q_A__kav> articles = new List<Basic_Technical_Q_A__kav>();

        for (Basic_Technical_Q_A__kav d : [SELECT Id, KnowledgeArticleId FROM Basic_Technical_Q_A__kav WHERE PublishStatus = 'draft' AND Language = 'en_US' AND ArticleNumber IN :articleNumbers]) {



            // Step 2: perform your updates

            d.validationstatus='Archived';

            articleIDs.add(d.KnowledgeArticleId);

            articles.add(d);

     system.debug('##tkarticles'+articles);

        }

       if(articles.size() > 0){

       update articles;

       }
       system.debug('##updatearticle'+articles);

       //Step 3: re-publish all the draft article of this batch.
    List<Id> tobearchivearticleIDs = new List<Id>();
       for (String articleId : articleIDs) {
       tobearchivearticleIDs.add(articleId);

            KbManagement.PublishingService.publishArticle(articleId, true);

        }

        for (String archivearticleId : tobearchivearticleIDs) {

    Datetime scheduledDate = NULL;
    KbManagement.PublishingService.archiveOnlineArticle(archivearticleId, scheduledDate);

        }



      }

    global void finish(Database.BatchableContext BC) {



    }



    }

Test Class

 @isTest 
    private class Basic_Article_type_test{
      static testMethod void MakeArticle() {
      test.startTest();


        User thisUser = [SELECT Id FROM User WHERE Id = :UserInfo.getUserId()];
        System.runAs (thisUser) 
        {
          Profile p = [SELECT Id FROM Profile WHERE Name='System Administrator ACN']; 
          User u = new User(Alias = 'standt', Email='test@test.com', 
            EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US', 
            LocaleSidKey='en_US', ProfileId = p.Id, 
            TimeZoneSidKey='America/Los_Angeles', UserName='sdss123@test.com');

          insert u;
          Basic_Technical_Q_A__kav newArticle = new Basic_Technical_Q_A__kav
          (Title='test article', UrlName='test-article', Language='en_US' ,Article_Confidentiality__c='confidential',Other__c='Not Product Relevant',Original_Information_Source__c='test info');


           // Insert Article
          insert newArticle;

 Basic_Technical_Q_A__kav newarticlekbid = [SELECT Id,KnowledgeArticleId FROM Basic_Technical_Q_A__kav WHERE PublishStatus = 'draft' AND Language = 'en_US' 
          AND ID =: newArticle.id];


          // Publish Article


          String articleId = newarticlekbid.KnowledgeArticleId;
          system.debug(articleId);
          KbManagement.PublishingService.publishArticle(articleId, true);

          // Archive Article
          Datetime scheduledDate = NULL;
    KbManagement.PublishingService.archiveOnlineArticle(articleId, scheduledDate);

          // Edit Article

          String id = KbManagement.PublishingService.editArchivedArticle(articleId);


         // RePublish Article
          String rearticleId = newarticlekbid.KnowledgeArticleId;
          system.debug(articleId);
          KbManagement.PublishingService.publishArticle(rearticleId, true);

           // ReArchive Article

          Datetime rescheduledDate = NULL;
    KbManagement.PublishingService.archiveOnlineArticle(rearticleId, rescheduledDate);







           test.stopTest();

          }
          }
    }
1

You need to call the Batch class in your Test class as follows within your StartTest and StopTest.

Database.executeBatch( new basic_articletype_batch(), 200 );
0

All you need to do to for the code coverage of the above batch is to execute it on your test method. Until you will execute it in your test method it will not run the batch for testing. Below is the code for the:

basic_articletype_batch bc = new basic_articletype_batch ();
Database.executeBatch(bc,200); //here 200 is the size of the batch, which is also the default batch

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.