You need to sign in to do that
Don't have an account?
shrey.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();
}
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();
}
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:
All Answers
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:
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;
}
}
}