+ Start a Discussion
Kumar GKumar G 

KnowledgeArticleVersionStandardController is not covering in test class

Please find my test class code in code sample and suggest me how to increase code coversge.
class :
public class NG_AgentContributionArticleController {
    // The constructor must take a ApexPages.KnowledgeArticleVersionStandardController as an argument
    public NG_AgentContributionArticleController(ApexPages.KnowledgeArticleVersionStandardController ctl) {
        SObject article = ctl.getRecord();   //this is the SObject for the new article. 
                                             //It can optionally be cast to the proper article type, e.g. FAQ__kav article = (FAQ__kav) ctl.getRecord();
        String sourceId = ctl.getSourceId(); //this returns the id of the case that was closed.
        Case c = [select subject, description, origin,Internal_only__c, Article_Requestor__c,
            Article_Sub_Classification__c from Case where id=:sourceId];
        article.put('title', c.subject);  //this overrides the default behavior of pre-filling the title of the article with the subject of the closed case. 

Test class :
private class Test_NG_AgentContributionArticlecntlr
static testMethod void Test_NG_AgentContributionArticlecntlr() {

         String caseSubject = 'title';         
         String caseDesc = 'Solution_Section_1__c';        
         String intlvagt = 'Solution_Section_2__c';         
         string intrnl = 'Internal_Only__c';         
         String orgn = 'Source__c'; 
         String atclcls = 'Article_Classification__c';         
         String atsbcls = 'Article_Sub_Classification__c';          
         String atreq = 'Case_Requestor__c';         
         String lang = 'Language';

         Case c = new Case();
         c.subject= caseSubject;
         c.description = caseDesc;
         c.Internal_Live_Agent_Notes__c = intlvagt;
         c.Internal_Only__c = False;
         c.Origin = orgn;
         c.Article_Classification__c = atclcls ;
         c.Article_Sub_Classification__c = atsbcls ;
         c.Article_Requestor__c = atreq ;
         //c.en_US = 'lang' ;
         insert c;
         String caseId = c.id;
         System.debug('Created Case: ' + caseId);
         ApexPages.currentPage().getParameters().put('sourceId', caseId);
         ApexPages.currentPage().getParameters().put('sfdc.override', '1');
         ApexPages.KnowledgeArticleVersionStandardController ctl = 
            new ApexPages.KnowledgeArticleVersionStandardController(c);
        try {
         new NG_AgentContributionArticleController (ctl);
        } catch(System.SObjectException e) {
            system.assertEquals(e.getMessage(), e.getMessage());

         System.assertEquals(caseId, ctl.getSourceId());
         //System.assertEquals('From Case: '+caseSubject, ctl.getRecord().get('title'));
         //System.assertEquals(caseDesc, ctl.getRecord().get('details__c'));

Uncovered code
v varaprasadv varaprasad
Hi Kumar,

After Case insertion Create knowledge article version code like below : 

//create the kav instance 
        FAQ__kav a = new FAQ__kav(
            Title = 'test apex',
            Summary = 'test from apex',
            Answer__c = 'test',
            Question__c = 'test',
            Category__c = 'test',
            URLName = 'test'
        insert a;
        //retrieve master article Id created on FAQ__kav record insertion
        //in order to get the KnowledgeArticleId
        a = [SELECT KnowledgeArticleId FROM FAQ__kav WHERE Id = :a.Id];
        //publish it
        KbManagement.PublishingService.publishArticle(a.KnowledgeArticleId, true);

Add above code after case insertion and check once.

I hope it helps you.
Please let me know in case of any other assistance.


Kumar GKumar G
Hi Vara Prasad,

I have added above code after case insertion , i am getting the error like : Error: Compile Error: Invalid type: FAQ__kav
Please suggest on the same.