+ Start a Discussion
ManojKumar MuthuManojKumar Muthu 

how to write a Test class for both Httppost and HttpGet?

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



 
sowmya Inturi 9sowmya Inturi 9
Hi Manoj,
You can create mock callouts in test class to cover you code.
Please follow the below link to do this:
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_callouts_wsdl2apex_testing.htm

If you are still facing issues, let me know. Will help you out.


Thanks,
Sowmya.
Raj VakatiRaj Vakati
Use this code
 
@isTest
private class CaseAttachmentTest {
    @isTest static void testCaseAttachmentGet() {
    
  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);
	 req.httpMethod = 'GET';
    RestContext.request = req;
    RestContext.response= res;
    CaseAttachment.getCaseById();
    Test.stopTest();

    }
    
 @isTest static void testCaseAttachmentPost() {
    
  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();
   
   //String JsonMsg=JSON.serialize([Select Id from Case]);

    req.requestURI = '/services/apexrest/CaseAccountid/';  //Request URL
    //req.requestBody = Blob.valueof(JsonMsg);
	 req.httpMethod = 'POST';
    RestContext.request = req;
    RestContext.response= res;
    CaseAttachment.doget('pdf','aaaaaaaaaaaaaaaaa','pdf',c.Id);
    Test.stopTest();



     
        
   
  
  
  
    }
	
	}