Skip to main content
Hi there,

I got in between a test class which is should cover both POST and Get Method, for my test the code coverage is just 35%

Can someone help to get 100% code coverage?

Below my apex class,

@RestResource(urlMapping='/CaseAttachment/*')

global with sharing class CaseAttachment{

@HttpGet

    global static List<Attachment> getCaseById() {

       RestRequest req = RestContext.request;

        RestResponse res = RestContext.response;

        String Id = req.requestURI.substring(req.requestURI.lastIndexOf('/')+1);

          List<Attachment> result =  [Select id, Name, OwnerId, BodyLength, LastModifiedById, LastModifiedDate, ContentType, Body, Description, CreatedDate, CreatedById from Attachment where ParentId = :Id];

        return result;

        }

           

@HttpPost

  global static void doget(String type, String body, String ContentType, String ParentID ){

        RestRequest req = RestContext.request;

        RestResponse res = RestContext.response;

        String CaseName=req.requestURI.substring(req.requestURI.lastIndexOf('/')+1);

        String doc=EncodingUtil.Base64Encode(req.requestBody);

         Attachment a = new Attachment();

            a.Name = type;

            a.Body = Blob.valueOf(body);           

            a.ContentType = ContentType;

            a.ParentId = 'Id';

                

        insert a;

        

    }

}

Test Class,

@isTest

private class CaseAttachmentTest {

    @isTest static void testCaseAttachmentOne() {

    

  Test.startTest();

  

  

    //create account

    Account acc = new Account();

    //enter details  

    acc.Name = 'Test Account';

    insert acc;

    

    //create case

    Case c = new Case();

    //enter details

    c.AccountId = acc.Id;

    c.Type = 'My Type';

    c.Origin = 'My Origin';

    c.Status = 'My Status';

    insert c;

  

   RestRequest req = new RestRequest(); 

   RestResponse res = new RestResponse();

    req.requestURI = '/services/apexrest/CaseAccountid/'+c.accountId;  //Request URL

    // req.requestBody = Blob.valueof(Body);

    RestContext.request = req;

    RestContext.response= res;

    CaseAttachment.getCaseById();

    Test.stopTest();

     

        

   

  

  

  

    }

    

}

TIA

 
2 respuestas
0/9000