+ Start a Discussion
Obulesh RajObulesh Raj 

how to write test class for class containing webservice variables

I have a class which contains other classes having webservice variables declared in them. How to write test class for the class?
Balayesu ChilakalapudiBalayesu Chilakalapudi
Use a StaticResourceCalloutMock built-in class to write test class for your webservice like this,
@isTest
private class CalloutStaticClassTest {
    @isTest static void testCalloutWithStaticResources() {
        // Use StaticResourceCalloutMock built-in class to
        // specify fake response and include response body 
        // in a static resource.
        StaticResourceCalloutMock mock = new StaticResourceCalloutMock();
        mock.setStaticResource('mockResponse');
        mock.setStatusCode(200);
        mock.setHeader('Content-Type', 'application/json');
        
        // Set the mock callout mode
        Test.setMock(HttpCalloutMock.class, mock);
        
        // Call the method that performs the callout
        HTTPResponse res = CalloutStaticClass.getInfoFromExternalService(
            'http://api.salesforce.com/foo/bar');
        
        // Verify response received contains values returned by
        // the mock response.
        // This is the content of the static resource.
        System.assertEquals('{"hah":"fooled you"}', res.getBody());
        System.assertEquals(200,res.getStatusCode());
        System.assertEquals('application/json', res.getHeader('Content-Type'));   
    }
}
Let me know if it helps to resolve your issue.

Thanks,
Balayesu