Introduction:
In Salesforce, the Apex programming language allows you to create powerful web services that can be exposed to external systems for data integration. One common scenario is using the @HttpPost annotation to create a custom RESTful web service. In this blog post, we'll walk through some examples of how to write effective test classes for an @HttpPost exposed web service class in Salesforce. Writing comprehensive test classes ensures that your code is robust, functional, and ready for deployment.
Example Scenario:
Let's consider a scenario where we have a custom object named "Expense__c" to track expenses, and we want to expose a RESTful web service to create new expense records using the @HttpPost annotation.
Web Service Class:
Here's a simple example of the ExpenseWebService class that exposes an @HttpPost method to create expense records:
@RestResource(urlMapping='/expenseService')
global with sharing class ExpenseWebService {
@HttpPost
global static String createExpense(String expenseName, Decimal amount) {
Expense__c newExpense = new Expense__c(Name = expenseName, Amount__c = amount);
insert newExpense;
return 'Expense record created with ID: ' + newExpense.Id;
}
}
Test Class:
Now, let's create a test class to thoroughly test the ExpenseWebService class. Here's an example of a test class that covers positive and negative test cases:
@isTest
private class TestExpenseWebService {
@isTest static void testCreateExpense() {
Test.startTest();
RestRequest req = new RestRequest();
RestResponse res = new RestResponse();
req.requestURI = '/services/apexrest/expenseService';
req.httpMethod = 'POST';
req.requestBody = Blob.valueOf('{"expenseName": "Sample Expense", "amount": 100.00}');
RestContext.request = req;
RestContext.response = res;
String result = ExpenseWebService.createExpense('Sample Expense', 100.00);
Test.stopTest();
System.assertEquals('Expense record created', result.substring(0, 25));
// Query the created expense record and perform additional assertions
List<Expense__c> expenses = [SELECT Id, Name, Amount__c FROM Expense__c WHERE Name = 'Sample Expense'];
System.assertEquals(1, expenses.size());
System.assertEquals('Sample Expense', expenses[0].Name);
System.assertEquals(100.00, expenses[0].Amount__c);
}
@isTest static void testCreateExpense_invalidData() {
Test.startTest();
RestRequest req = new RestRequest();
RestResponse res = new RestResponse();
req.requestURI = '/services/apexrest/expenseService';
req.httpMethod = 'POST';
req.requestBody = Blob.valueOf('{"expenseName": "", "amount": -50.00}');
RestContext.request = req;
RestContext.response = res;
String result = ExpenseWebService.createExpense('', -50.00);
Test.stopTest();
System.assertEquals('Error creating expense', result.substring(0, 23));
}
}
Conclusion:
Writing effective test classes for @HttpPost exposed web service classes in Salesforce is crucial to ensure the reliability and functionality of your code. The provided examples cover both positive and negative test cases, allowing you to validate the behavior of your web service under various scenarios. By following these practices, you can create robust and well-tested web services for seamless data integration in your Salesforce applications.