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

REST API Test class code coverage issue

Hi All,

I am trying to write test class for my REST API (POST method) class, i didn't get any errors but i.e showing 0% code coverage. Below are my class and test class.

API class:

global Class AddComment{
   global static string addComment(){
       String statuscode = '1';
       String datetimevalue;
       String  JSONString;
       //Map for returning output Response
       Map<String, String> returnMap = new Map<String, String>();
             RestRequest req = RestContext.request;
             RestResponse res = RestContext.Response;
             Blob body = req.requestBody;
             String bodyString = body.toString();
              Wrapclass deserializedinput = (Wrapclass)JSON.deserialize(bodyString, Wrapclass.class);
             Case parentCase = [ Select ID, CaseNumber from Case where CaseNumber =: deserializedinput.Casenum];
             CaseComment Commentobject = new CaseComment();
             Commentobject.ParentId = parentCase.Id;
             Commentobject.CommentBody = deserializedinput.Description;
             insert Commentobject;
            CaseComment CreatedComment = [Select a.LastModifiedById,LastModifiedDate From CaseComment a where id =:Commentobject.Id];
             Datetime dttime =CreatedComment.LastModifiedDate;
             datetimevalue = string.valueOf(dttime);
                statuscode = '0';
                 returnMap.put('CreatedDatetime', datetimevalue);
                 JSONString = JSON.serialize(returnMap);
                 System.debug('+++Output details+++'+JSONString);
                 return JSONString  ;
    public class Wrapclass{
      public string Casenum;
      public string Description;


Test class:

Public class AddCommenttest{
    public static void AddCommenttest(){
        Case cs = new Case(status = 'open');
        insert cs;
        Case csnum = [select casenumber from Case where Id =: cs.Id];
        AddComment.Wrapclass reqst=new AddComment.Wrapclass();
        reqst.Casenum= csnum.casenumber;
        reqst.Description ='Test comment';
        String JsonMsg=JSON.serialize(reqst);

        RestRequest req = new RestRequest();
        RestResponse res = new RestResponse();

        req.requestURI =    ''; 
        req.httpMethod = 'POST';
        req.requestBody = Blob.valueof(JsonMsg);
        RestContext.response= res;
        RestContext.request = req;
        String returnMap = AddComment.addComment();

Thanks in Advance!!



This is a known issue in winter 14 release.

Workaround:  Edit and Save the test class or Compile all classes from the Apex class list page.

The results of overall code coverage are correct when running a deployment. If you wish to see theoverall coverage and whether tests are passing, please run a deployment with validate only selected to run the tests and get the results.

The individual code coverage for classes and triggers can be determined using Developer Console.