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
shrey.tyagi88@tcs.comshrey.tyagi88@tcs.com 

Test Class for @HTTP Post

Hi Everyone,
         I have written a @HTTP Post method which works fine. I need a sample code to write it's test class . Can anyone  please help me with this? My test method is giving an error saying that method doPost() does not exist (second last line).

Apex Code:

@RestResource(urlMapping='/v1/cans/*')
global with sharing class MyRestResource {
  @HttpPost
    global static DocWrapper doPost(String Description, String ParentId, String FilePathname, String Name, String Type) {
        DocWrapper response=new DocWrapper();
        Document__c doc= new Document__c();
        doc.Doc_Name__c=Name;
        doc.Type__c=Type;
        doc.CAN__c=ParentId;
        doc.Link__c=FilePathname;
        doc.Description__c=Description;
        insert doc;
        response.message='Doc Inserted';
        return response;
        
     }
       global class DocWrapper {          
          public String message;
     }
}

Test Method:


static testMethod void testDoPost(){
        
        RestRequest req = new RestRequest(); 
        RestResponse res = new RestResponse();
        
        RecordType RecType = [Select Id From RecordType  Where SobjectType = 'CAN__c' and Name = 'CAN Fed'];    
        CAN__c cf = new CAN__c (Name='Test CAN 1',RecordTypeId=RecType.Id);
        insert cf;
        String CanId=cf.id;
        
        req.requestURI = '/services/apexrest/v1/cans/';  
        req.addParameter('Description', '0000000');
        req.addParameter('ParentId',CanId);
        req.addParameter('FilePathname', 'www.google.com');
        req.addParameter('Name', '1wer2547');
        req.addParameter('Type', '1wer2547');

        req.httpMethod = 'POST';
        RestContext.request = req;
        RestContext.response = res;
        
        Test.startTest();
        MyRestResource.DocWrapper results = MyRestResource.doPost();
        Test.stopTest();
        

    }
Best Answer chosen by shrey.tyagi88@tcs.com
Tadeas BTadeas B
Hi,
the problem is that your doPost method is expecting some parameters (String Description, String ParentId, String FilePathname, String Name, String Type).

Try something like this:
Test.startTest();
MyRestResource.DocWrapper results = MyRestResource.doPost('000000', CanId, 'www.google.com', '1wer2547', '1wer2547');
Test.stopTest();

 

All Answers

Tadeas BTadeas B
Hi,
the problem is that your doPost method is expecting some parameters (String Description, String ParentId, String FilePathname, String Name, String Type).

Try something like this:
Test.startTest();
MyRestResource.DocWrapper results = MyRestResource.doPost('000000', CanId, 'www.google.com', '1wer2547', '1wer2547');
Test.stopTest();

 
This was selected as the best answer
shrey.tyagi88@tcs.comshrey.tyagi88@tcs.com
Thanks Dude!!!!
SFDCDEVELOPERSFDCDEVELOPER
The above test class is not working for my class please let me know why
here is my class
@RestResource(urlMapping='/PostMethod/*')
global with sharing class myrestResource {

 @HttpPost   
 global static void createNewRecordl(String Email, String FirstName,String LastName) {
     
try{
     List<Person__c> tr= [Select ID, Name, Email__c from Person__c where Email__c = :Email];
     system.debug('----------'+ tr.size());
        
     if(tr.size() > 0) {
     tr[0].First_Name__c=FirstName;
     tr[0].Last_Name__c=LastName;
     update tr[0];
        Additionaldata__c tjr =new Additionaldata__c ();
      
        tjr.Email__c=Email;
        insert tjr;
        RestContext.response.addHeader('Content-Type', 'application/json');
        RestContext.response.responseBody = Blob.valueOf(JSON.serialize(tjr));
       }
     
     if(tr.size() == 0) {
        Person__c tr1= new Person__c();
        tr1.Email__c =Email;
        tr1.First_Name__c=FirstName;
        tr1.Last_Name__c=LastName;
     
        insert tr1;
        system.debug('----------'+ tr1);
        Additionaldata__c  tjr1 =new Additionaldata__c ();
    
        tjr1.Email__c=Email;
        insert tjr1;
        RestContext.response.addHeader('Content-Type', 'application/json');
        RestContext.response.responseBody = Blob.valueOf(JSON.serialize(tr1)); 
        RestContext.response.responseBody = Blob.valueOf(JSON.serialize(tjr1));     
     }
        }catch(Exception e){
            // TODO Formalize as a class if this structure proves to be durable.
            String jsonStr = 
                '{' + 
                    '"errorCode": "' + e.getTypeName() + '", ' + 
                    '"message": "' + e.getMessage() + '"' + 
                '}';
            RestContext.response.addHeader('Content-Type', 'application/json');
            RestContext.response.responseBody = Blob.valueOf(jsonStr); 
            RestContext.response.statuscode = 500;
        }       
  }  
}