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
Sanjay WadgeSanjay Wadge 

Need to write a test class

Hi,

I have written following my first Apex class. Need help in writing a test class to meet the code coverage.

public without sharing class UpdateKBArticleNoOnCase {
    
    public class UpdateKBArticleException extends Exception {}
    
    public static void handleArticleNoUpdate () {

   DateTime rightNow =  DateTime.now();
      DateTime d24hAgo = rightNow.addHours(-24);
      
      // Get unique cases for which articles were added....
      
      Set<Id> setid = new Set<Id>();
    
      // for (CaseArticle CasesWithArticle : [Select CaseId from CaseArticle where CreatedDate = YESTERDAY])
      for (CaseArticle CasesWithArticle : [Select CaseId from CaseArticle where CreatedDate > :d24hAgo])
      { setid.add(CasesWithArticle.CaseId); }

      // for (Case cases : [Select Id, KB_Article__c from Case IN CasesWithArticle])
      for (Case cases : [Select Id, KB_Article__c from Case WHERE Id IN :setid])
      {
        
        String AllArticleNos;
        Integer i=0;
        
        List<String> ArticleNumbers = new List<String>();
        for (CaseArticle Articles : [Select KnowledgeArticle.ArticleNumber FROM CaseArticle 
             where CaseId = :cases.Id])
        { ArticleNumbers.add(Articles.KnowledgeArticle.ArticleNumber); 
          if (i == 0)
             AllArticleNos = Articles.KnowledgeArticle.ArticleNumber;
          else
             AllArticleNos += ', '+Articles.KnowledgeArticle.ArticleNumber;
          i++;
        }
        
        System.debug(cases.id + ' ' + AllArticleNos);
                 
        // Update Case
        
        cases.KB_Article__c = AllArticleNos;
        update cases;
    
       }
    }
}

I have seen insert test examples but did not find any update one. Please help in writing test class for the above mention code as well as steps to test the aboe code.
Thanks .
ShashankShashank (Salesforce Developers) 
Please see if this helps: https://developer.salesforce.com/page/An_Introduction_to_Apex_Code_Test_Methods