You need to sign in to do that
Don't have an account?
Steve Cairney
Test class for Web service class?
Hi, I'm in the the perculiar position where I have to try and write a test class for a web service class that was written by someone else.
I seem to be finding conflicting opinions on whether a test class will actually give the code coverage needed.
So my question is this, given the following code (which I didn't write) what's the best way to get the web service class ready and covered to send to our production org?
I seem to be finding conflicting opinions on whether a test class will actually give the code coverage needed.
So my question is this, given the following code (which I didn't write) what's the best way to get the web service class ready and covered to send to our production org?
global with sharing class ItsApprovedWebServices { //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // Description : Web Service to login to ItsApproved // Called From : "Test Submit Booking" botton on ItsApproved Booking Page // Returns : Authorization Token (for use in subsequent web service calls to Its Approved) //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// webservice static String Login(String userName) { HttpRequest req = new HttpRequest(); HttpResponse res = new HttpResponse(); Http http = new Http(); req.setEndpoint('http://stagingv4ws.itsapproved.net/api/values/login?username=cestrianMAG'); req.setMethod('GET'); req.setTimeout(60000); //req.setHeader('Content-Type','application/json'); //req.setBody('username'+EncodingUtil.urlEncode(userName, 'UTF-8')); //req.setBody(username); // try { res = http.send(req); // } catch(System.CalloutException e) { // System.debug('Callout error: '+ e); // System.debug(res.toString()); // } return res.getHeader('Authorization-Token'); } //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // Description : Web Service to create representation of Booking in JSON format // Called From : "Test Submit Booking" botton on ItsApproved Booking Page // Returns : Complete Booking as a JSON object //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// webservice static String GetBookingDetails(ID id) { JSONGenerator jsonGenerator = JSON.createGenerator(true); ItsApproved_Booking__c booking = [SELECT Id, Booking_Title__c, Customer_Reference__c, Incharge_Date__c FROM ItsApproved_Booking__c WHERE Id = :id]; jsonGenerator.writeStartObject(); // Booking jsonGenerator.writeStringField('CompanyId', '798365d8-4278-4b16-a330-c4a3a9e1f91f'); jsonGenerator.writeNumberField('UserId', 84); //jsonGenerator.writeStringField('BookingId', booking.Id); jsonGenerator.writeStringField('BookingTitle', booking.Booking_Title__c); jsonGenerator.writeStringField('CustomerReference', booking.Customer_Reference__c); jsonGenerator.writeStringField('CustomerBookingId', booking.Id); jsonGenerator.writeDateTimeField('InchargeDate', booking.Incharge_Date__c); List<ItsApproved_Product__c> products = [SELECT Id, Product_Code__c, Booking_Reference__c FROM ItsApproved_Product__c WHERE Booking_Reference__c = :Id]; jsonGenerator.writeFieldName('Products'); jsonGenerator.writeStartArray(); // Products for(ItsApproved_Product__c p: products) { jsonGenerator.writeStartObject(); // Products //jsonGenerator.writeStringField('ProductId', p.Id); jsonGenerator.writeStringField('ProductionCode', p.Product_Code__c); jsonGenerator.writeStringField('CustomerProductReference', p.Product_Code__c); //jsonGenerator.writeStringField('CustomerProductReference', p.Booking_Reference__c); List<ItsApproved_Design__c> designs = [SELECT Id, Artwork_Title__c FROM ItsApproved_Design__c WHERE Product_Reference__c = :p.Id]; jsonGenerator.writeFieldName('Designs'); jsonGenerator.writeStartArray(); // Designs for(ItsApproved_Design__c d: designs) { jsonGenerator.writeStartObject(); jsonGenerator.writeStringField('CustomerDesignId', d.Id); jsonGenerator.writeStringField('DesignName', d.Artwork_Title__c); List<ItsApproved_Delivery__c> deliveries = [SELECT Depot_Code__c, Quantity__c FROM ItsApproved_Delivery__c WHERE ItsApproved_Design__c = :d.Id]; jsonGenerator.writeFieldName('Deliveries'); jsonGenerator.writeStartArray(); // Deliveries for(ItsApproved_Delivery__c del: deliveries) { jsonGenerator.writeStartObject(); // Deliveries jsonGenerator.writeStringField('DeliveryId', del.Id); jsonGenerator.writeNumberField('DepotCode', del.Depot_Code__c); jsonGenerator.writeNumberField('Quantity', del.Quantity__c); jsonGenerator.writeEndObject(); // Deliveries } jsonGenerator.writeEndArray(); // Deliveries jsonGenerator.writeEndObject(); // Designs } jsonGenerator.writeEndArray(); // Designs jsonGenerator.writeEndObject(); // Products } jsonGenerator.writeEndArray(); // Products jsonGenerator.writeEndObject(); // Booking return jsonGenerator.getAsString(); } //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // Description : Web Service to send complete Booking Details to Its Approved // Called From : "Test Submit Booking" botton on ItsApproved Booking Page // Returns : from Web Service //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// webservice static String PostBooking(String json, String authorizationToken) { // Now send the data to Its Approved HttpRequest req = new HttpRequest(); HttpResponse res = new HttpResponse(); Http http = new Http(); req.setEndpoint('http://stagingv4ws.itsapproved.net/api/values/PostBooking'); req.setMethod('POST'); req.setHeader('Content-Type','application/json'); req.setHeader('Authorization-Token', authorizationToken); req.setBody(json); req.setTimeout(60000); // try { res = http.send(req); // } catch(System.CalloutException e) { // System.debug('Callout error: '+ e); // System.debug(res.toString()); // } //return res.toString(); return res.getBody(); } @future (callout=true) public static void TestReceiveNoLogonGet(String value) { HttpRequest req = new HttpRequest(); HttpResponse res = new HttpResponse(); Http http = new Http(); req.setEndpoint('http://stagingv4ws.itsapproved.net/api/values/TestReceiveNoLogonGet'); req.setMethod('GET'); //req.setBody('name='+EncodingUtil.urlEncode(value, 'UTF-8')); req.setCompressed(true); // otherwise we hit a limit of 32000 try { res = http.send(req); } catch(System.CalloutException e) { System.debug('Callout error: '+ e); System.debug(res.toString()); } } @future (callout=true) public static void TestReceiveNoLogonPost(String productNames) { HttpRequest req = new HttpRequest(); HttpResponse res = new HttpResponse(); Http http = new Http(); req.setEndpoint('http://stagingv4ws.itsapproved.net/api/values/TestReceiveNoLogonGet'); req.setMethod('POST'); req.setBody('model='+EncodingUtil.urlEncode(productNames, 'UTF-8')); req.setCompressed(true); // otherwise we hit a limit of 32000 try { res = http.send(req); } catch(System.CalloutException e) { System.debug('Callout error: '+ e); System.debug(res.toString()); } } webservice static String TestReturnBookingTitle(String value) { return value; } }
So you don't actually have to write a test class for the web service code, you need to create the HttpCalloutMock test and test that?
That is actually webservice http callout class i implemented in my ORG.
HTTPResponse res = CalloutAccountRequest.basicAuthCalloutRequest(); [this one right];
Thanks
V varaprasad
Please check once below code may useful to you.
For callouts we need to implement HttpCalloutMock interface then we need to call in test class.
@isTest
global class Test_MockHttpResponseGenerator implements HttpCalloutMock {
// Implement this interface method
global HTTPResponse respond(HTTPRequest req) {
// Optionally, only send a mock response for a specific endpoint
// and method.
System.assertEquals('https://xxxxxxxx.com', req.getEndpoint());
System.assertEquals('POST', req.getMethod());
// Create a fake response
HttpResponse res = new HttpResponse();
res.setHeader('Content-Type', 'application/xml');
res.setBody('{"foo":"bar"}');
res.setStatusCode(200);
return res;
}
}
==================================test class for callout =========================================================================
@isTest(seeAlldata = true)
public class Test_CalloutAccountRequest {
@istest public static void main5(){
Test.setMock(HttpCalloutMock.class, new Test_MockHttpResponseGenerator());
HTTPResponse res = CalloutAccountRequest.basicAuthCalloutRequest();
}
}
======================================================================================================================================