You need to sign in to do that
Don't have an account?

Could you please tell me how to write test class for restresource api
@RestResource(urlMapping='/sfdcCallingContact/*')
global class GetContactFromUKOrg{
@HttpPost
global static String insertContact()
{
System.debug('entered..'+RestContext.request.requestBody.toString().trim());
String jsonBody = RestContext.request.requestBody.toString().trim();
System.debug('jsonBody'+jsonBody );
List<Contact> ConObj = (List<Contact>)JSON.deserialize(jsonBody,List<Contact>.class);
List<Contact> ConObjInsert = new List<Contact>();
List<Contact> ConObjUpdate = new List<Contact>();
Iterator<Contact> keys = ConObj.iterator();
while(keys.hasNext()){
Contact key = keys.next();
if(key.Id != null){
ConObjUpdate.addAll(Conobj);
}else {
ConObjInsert.addAll(ConObj);
}
}
try{
if(ConObjUpdate != null){
update ConObjUpdate;
}
if(ConObjInsert != null){
insert ConObjInsert;
return JSON.serialize(ConObjInsert);
}
}catch(Exception e){
return JSON.serialize('Error while creating Contact');
}
return 'Success';
}
}
global class GetContactFromUKOrg{
@HttpPost
global static String insertContact()
{
System.debug('entered..'+RestContext.request.requestBody.toString().trim());
String jsonBody = RestContext.request.requestBody.toString().trim();
System.debug('jsonBody'+jsonBody );
List<Contact> ConObj = (List<Contact>)JSON.deserialize(jsonBody,List<Contact>.class);
List<Contact> ConObjInsert = new List<Contact>();
List<Contact> ConObjUpdate = new List<Contact>();
Iterator<Contact> keys = ConObj.iterator();
while(keys.hasNext()){
Contact key = keys.next();
if(key.Id != null){
ConObjUpdate.addAll(Conobj);
}else {
ConObjInsert.addAll(ConObj);
}
}
try{
if(ConObjUpdate != null){
update ConObjUpdate;
}
if(ConObjInsert != null){
insert ConObjInsert;
return JSON.serialize(ConObjInsert);
}
}catch(Exception e){
return JSON.serialize('Error while creating Contact');
}
return 'Success';
}
}
You can write test class like below.
Note:- You may need make some changes. You can modify this test class as per your requirement.
Also, refer below link for more info.
http://cloudyworlds.blogspot.in/2012/12/writing-test-classes-for-apex-rest.html
Let me know if this helps :)
Thanks,
Amit Singh
Seems that your JSON file that you have inserted into Apex test clas in invalid.
Please make sure that you are using the same JSON as you are using at the time of testing the functionaloty.
Thanks
AMit Singh