Discussions
- General Development
- Schema Development
- Apex Code Development
- Visualforce Development
- Formulas & Validation Rules
- Security
- Mobile
- Force.com Sites & Site.com
- Chatter Development
- Java Development
- .NET Development
- Perl, PHP, Python & Ruby
- Desktop Integration
- APIs and Integrations
- Visual Workflow
- Apple, Mac and OS X
- VB and Office Development
- AppExchange Directory & Packaging
- Salesforce Labs & Open Source Projects
- Other Salesforce Applications
- Jobs Board
- Force.com Discussion Boards
- :
- Developer Boards for Force.com and Database.com
- :
- APIs and Integration
- :
- Re: How to unit test v24 Apex REST classes?
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic to the Top
- Bookmark
- Subscribe
- Printer Friendly Page
Re: How to unit test v24 Apex REST classes?
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
11-29-2012 10:27 AM
You can mock the POST response message in JSONMsg
Something like
String JSONMsg = '{"message":"success"}';
Kartik Viswanadha | Force.com Developer | @logontokartik | http://peregrinusforce.com
Re: How to unit test v24 Apex REST classes?
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
11-29-2012 03:03 PM
I'm not having any luck. Here's my current error: Method does not exist or incorrect signature.
Here's my test code:
static testMethod void testdoPost() {
RestRequest req = new RestRequest();
req.addParameter('firstname','Wain');
req.addParameter('lastname','Rolen');
req.addParameter('company','Test Company');
req.addParameter('email','Wain@test.com');
req.addParameter('phone','6884382997');
req.addParameter('street','123 Test Ave');
req.addParameter('city','Somewhere');
req.addParameter('state','MA');
req.addParameter('zip','87945');
req.addParameter('country','US');
req.addParameter('password','T3st789');
req.addHeader('Content-Type', 'application/json'); // Add a JSON Header as it is validated
req.requestURI = 'https://XXX.salesforce.com/services/apexrest/leads /';
req.httpMethod = 'POST';
String JSONMsg = '{"message":"success"}';
req.requestBody = Blob.valueof(JSONMsg); // Add JSON Message as a POST
RestResponse res = new RestResponse();
RestContext.request = req;
RestContext.response = res;
LeadRestSvc.doPost();
} I'm wondering if I need to be using requestBody rather than addparameter given the below code?
global with sharing class LeadRestSvc {
@HttpPost
global static String doPost(String firstname, String lastname, String email, String password, String company, String phone, String street, String city, String state, String zip, String country) {
//code here
}
}
Re: How to unit test v24 Apex REST classes?
[ Edited ]- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
11-30-2012 07:18 AM - edited 12-01-2012 10:43 PM
static testMethod void testdoPost() {
Lead l=new Lead();
l.firstname='Wain';
l.lastname='Rolen';
l.company='Test Company';
l.email='Wain@test.com';
l.phone='6884382997';
l.street='123 Test Ave';
l.city='Somewhere';
l.state='MA';
l.zip='87945';
l.country='US';
l.password='T3st789';
String JSONMsg= JSON.serialize(l);
RestRequest req = new RestRequest();
req.addHeader('Content-Type', 'application/json'); // Add a JSON Header as it is validated
req.requestURI = '/services/apexrest/leads/';
req.httpMethod = 'POST';
req.requestBody = Blob.valueof(JSONMsg); // Add JSON Message as a POST
RestResponse res = new RestResponse();
RestContext.request = req;
RestContext.response = res;
LeadRestSvc.doPost('Wain','colin'...fill all the parameters); }Please try the above .I hope this is clear and also use Test.Start Test and Test.Stop Test Methods for test classes as best practice.Thanks
CERTIFIED SFDC ADMIN,CERTIFIED SFDC DEVELOPER,CERTIFIED SALESCLOUD and CERTIFIED SERVICE CLOUD CONSULTANT.
Re: How to unit test v24 Apex REST classes?
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
12-03-2012 11:25 AM
Thank you, Mohith! Your example worked well for my doPost method.
Now I'm using your example to attend a test method for a doPatch call but having issues with the test code not firing. Here is my code if you have any suggestions.
static testMethod void testdoPatch() {
Lead l = new Lead();
l.firstname = 'Wain';
l.lastname = 'Rolen';
l.company = 'Test Company';
l.email = 'Wain@test.com';
l.phone = '6884382997';
l.street = '123 Test Ave';
l.city = 'Somewhere';
l.state = 'MA';
l.PostalCode = '87945';
l.country = 'US';
l.Self_Service_Password__c = 'T3st789';
insert(l);
String recordId = l.sample_Id__c;
String JSONMsg = JSON.serialize(l);
RestRequest req = new RestRequest();
req.addHeader('Content-Type', 'application/json'); // Add a JSON Header as it is validated
req.requestURI = '/services/apexrest/v.1/leads/' + recordId;
req.httpMethod = 'PATCH';
req.requestBody = Blob.valueof(JSONMsg); // Add JSON Message as a POST
RestResponse res = new RestResponse();
RestContext.request = req;
RestContext.response = res;
LeadRestSvc.doPatch('Wain','Rolen','Wain@test.com' ,'6884382997','12345 Test Ave','Somewhere','MA','87945','US');
}
Re: How to unit test v24 Apex REST classes?
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
12-09-2012 11:29 PM
Please paste the class for PATCH call.I will have a look .Thanks.Please Mark as answer for POST call as that may help others .
CERTIFIED SFDC ADMIN,CERTIFIED SFDC DEVELOPER,CERTIFIED SALESCLOUD and CERTIFIED SERVICE CLOUD CONSULTANT.
Re: How to unit test v24 Apex REST classes?
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
12-10-2012 08:49 AM
Here is the PATCH call:
@HttpPatch
global static String doPatch(String firstname, String lastname, String email, String phone, String street, String city, String state, String zip, String country) {
RestRequest req = RestContext.request;
// see if a wicell_id was part of the uri
String sample_id = req.requestURI.substring(req.requestURI.lastIndexO f('/')+1);
if (sample_id != '') {
Savepoint sp = Database.setSavePoint();
try {
Contact[] contactToUpdate = [SELECT Id FROM Contact WHERE Sample_Id__c =: sample_id];
if(contactToUpdate.size() > 0){
for(Contact c : contactToUpdate){
c.FirstName = firstname;
c.LastName = lastname;
c.Email = email;
c.Phone = phone;
c.MailingStreet = street;
c.MailingCity = city;
c.MailingState = state;
c.MailingPostalCode = zip;
c.MailingCountry = country;
}
update contactToUpdate;
}
else {
Lead[] leadToUpdate = [SELECT Id FROM Lead WHERE IsConverted = false AND Sample_Id__c =: sample_id];
if(leadToUpdate.size() > 0){
for(Lead l : leadToUpdate){
l.FirstName = firstname;
l.LastName = lastname;
l.Email = email;
l.Phone = phone;
l.Street = street;
l.City = city;
l.State = state;
l.PostalCode = zip;
l.Country = country;
}
update leadToUpdate;
}
}
return sample_id;
}
catch (DMLException e1) {
Database.rollback(sp);
return e1.getDMLMessage(0);
}
catch (Exception e2) {
Database.rollback(sp);
return e2.getMessage();
}
}
else {
return 'Invalid operation';
}
}Below is the latest test code for the PATCH call. Note, I'm getting the following error: List has no rows for assignment to SObject on the following line about half way through the below code: String orgId = [SELECT Id from Account LIMIT 1].Id;
I have @isTest listed at the top of my class.
static testMethod void testdoPatch() {
Lead l = new Lead();
l.firstname = 'Wain';
l.lastname = 'Rolen';
l.company = 'Test Company';
l.email = 'Wain@test.com';
l.phone = '6884382997';
l.street = '123 Test Ave';
l.city = 'Somewhere';
l.state = 'MA';
l.PostalCode = '87945';
l.country = 'US';
insert(l);
String sample_Id = l.Sample_Id__c;
String JSONMsg = JSON.serialize(l);
RestRequest req = new RestRequest();
req.addHeader('Content-Type', 'application/json'); // Add a JSON Header as it is validated
req.requestURI = 'https://cs4.salesforce.com/services/apexrest/v.1/l eads/' + sample_Id;
req.httpMethod = 'PATCH';
req.requestBody = Blob.valueof(JSONMsg); // Add JSON Message as a POST
RestResponse res = new RestResponse();
RestContext.request = req;
RestContext.response = res;
LeadRestSvc.doPatch('Wain','Rolen','Wain@test.com' ,'6884382997','12345 Test Ave','Somewhere','MA','87945','US');
//Test doPatch for contacts
String orgId = [SELECT Id from Account LIMIT 1].Id;
Contact c = new Contact();
c.firstname = 'Wain';
c.lastname = 'Rolen';
c.AccountId = orgId;
c.email = 'Wain@test.com';
c.phone = '6884382997';
c.mailingstreet = '123 Test Ave';
c.mailingcity = 'Somewhere';
c.mailingstate = 'MA';
c.mailingPostalCode = '87945';
c.mailingcountry = 'US';
c.Sample_Id__c = sample_Id;
insert(c);
String contactSampleId = c.Sample_Id__c;
String JSONMsg2 = JSON.serialize(c);
RestRequest req2 = new RestRequest();
req2.addHeader('Content-Type', 'application/json'); // Add a JSON Header as it is validated
req2.requestURI = 'https://cs4.salesforce.com/services/apexrest/v.1/l eads/' + contactSampleId;
req2.httpMethod = 'PATCH';
req2.requestBody = Blob.valueof(JSONMsg2); // Add JSON Message as a POST
RestResponse res2 = new RestResponse();
RestContext.request = req2;
RestContext.response = res2;
LeadRestSvc.doPatch('Wain','Rolen','Wain@test.com' ,'6884382997','12345 Test Ave','Somewhere','MA','87945','US');
}I tried to mark your earlier response as the answer but I wasn't given the option. Maybe this is due to a different post being marked as the answer by another user?
Thank you for your help!!!
Re: How to unit test v24 Apex REST classes?
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
12-15-2012 03:58 AM
Insert the account in your test class .And then the exception will disappear
CERTIFIED SFDC ADMIN,CERTIFIED SFDC DEVELOPER,CERTIFIED SALESCLOUD and CERTIFIED SERVICE CLOUD CONSULTANT.
Re: How to unit test v24 Apex REST classes?
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
01-02-2013 09:36 AM
Thank you, Mohit! That solved the problem.
Most of my PATCH code is not being tested. I think it's because I'm creating new lead and contact records in the tests but then the following lines do not return anything for the new test sample_ids I'm sending.
Contact[] contactToUpdate = [SELECT Id FROM Contact WHERE Sample_Id__c =: sample_id];
Lead[] leadToUpdate = [SELECT Id FROM Lead WHERE IsConverted = false AND Sample_Id__c =: sample_id];
Re: How to unit test v24 Apex REST classes?
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
02-13-2013 07:14 AM
Here is a screen shot of the patch code (click "Test Code" hyperlink) that is not being fired by my test code (included below). Does anyone have thoughts around why line 68 is returning false?
Here is my test code:
static testMethod void testdoPatch() {
//create new order
//Create New Account (Org) Record to link to Opportunity
Account newAcct = new Account();
newAcct.Name = 'Bubba Gump Shrimp Co';
newAcct.RecordTypeId = '012500000005HjFAAU';
insert(newAcct);
Id OrgId = newAcct.Id;
//Create New Account (Org) Record for TTO
Account newAct = new Account();
newAct.Name = 'Company';
newAct.RecordTypeId = '012500000005HjFAAU';
newAct.Agreement_Company_Code__c = 'W';
insert(newAct);
//Create New Contact Record to link to Account
Contact newCon = new Contact();
newCon.FirstName = 'Tommy';
newCon.LastName = 'Tuckerr';
insert(newCon);
Id ContactId = newCon.Id;
//Create New Opportunity Record
Id oppRT = [SELECT Id FROM RecordType WHERE SobjectType = 'Opportunity' and IsActive = True LIMIT 1].Id;
Opportunity newOpp = new Opportunity();
newOpp.AccountId = OrgId;
newOpp.Licensing_Organization__c = OrgId; //'0015000000LQdcMAAT';
newOpp.Opportunity_Number__c = 'D09-J0001';
newOpp.RecordTypeId = oppRT;
newOpp.Name = 'OPPORTUNITY NAME';
newOpp.StageName = 'Initial Contact/Exploration';
newOpp.CloseDate = date.Today() + 30;
newOpp.Clone_Flag__c = True;
newOpp.Agreement_Clone_ID__c = 'a0750000002zlJYAAY';
insert(newOpp);
Id NewOppId = newOpp.Id;
//Create New Order Record
Id orderRT = [SELECT Id FROM RecordType WHERE SobjectType = 'Order__c' and IsActive = True LIMIT 1].Id;
Order__c odr = new Order__c();
odr.RecordTypeId = orderRT;
odr.Name = '9998989';
odr.Opportunity_Name__c = NewOppId;
odr.Status__c = 'Open'; //'Opportunity Created';
odr.Account__c = OrgId;
odr.Contact__c = ContactId;
insert(odr);
String odrID = odr.Name;
RestRequest req = new RestRequest();
req.addHeader('Content-Type', 'application/json'); // Add a JSON Header as it is validated
req.requestURI = 'https://cs4.salesforce.com/services/apexrest/v.1/o rders/' + odrID +'/';
req.httpMethod = 'PATCH';
RestResponse res = new RestResponse();
RestContext.request = req;
RestContext.response = res;
OrderRestSvc.doPatch();
}

