I wrote a web service class. From my understanding unit tests in salesforce are supposed to test the whole functionality (end to end). But I don't know how to test this external interface.

Therefore my question is:

  • Am I supposed to call the web service class from outside?
  • How to Write a Test Class for Webservice Class?


   global class ObjectFieldUpdate2{  

    global class RequiredFields{
         webservice String FieldName; 
         webservice String FieldValue;

    webservice static String FiledtoUpdate2(String ObjName,Integer ExtId, List<RequiredFields> fieldvalues){
        String str='';
        Schema.SObjectType t = Schema.getGlobalDescribe().get(ObjName);
            String query = 'select Id' + ' from ' + ObjName +' where External_Id__c ='+ExtId+' Limit 1';
            SObject s = Database.query(query);
               SObject sObjectUpdate = t.newSObject(s.Id);
                    for(RequiredFields flds : fieldvalues)      
                       sObjectUpdate.put(flds.FieldName, flds.FieldValue);

                      Update sObjectUpdate;

                }catch(Exception e){
            return 'Success';
            return str;         

  • 1
    it is not a duplicate. He is not asking for mocking a web service... – Christian Deckert May 5 '15 at 10:42
  • 1
    What are you struggling with ? You haven't really asked us a specific question. You can test these methods like any other methods. Please update your question to share with us what you're not understanding about testing webservices. – Samuel De Rycke May 5 '15 at 11:23
  • Hey I think that the question is now more specific.... – Christian Deckert May 5 '15 at 14:52

Welcome to the community SFDC developer!


In Salesforce it is possible to use unit tests to make sure that the implemented code unit works as expected for a given scenario. All tests should be performed with data which was created for the test scenario. The usage of existing data is allowed but not recommended. However, in order to run tests in an encapsulated environment you are not allowed to use callouts to other services. (This could result in inconsistencies.. Therefore you have to mock these external services using HTTPCalloutMock).


However, in your case you implemented a global class which is exposed as a web service. You are not allowed to call web services during tests. Therefore it is not possible to call your own service from outside to test it. My suggestion therefore is that you implement a standard unit test (like all the other unit test, which implements a simple test).

The test class could look like this:

What follows is pseudo code.

class ObjectFieldUpdate2_Test
    private static void test()
        ObjectFieldUpdate2_Test.RequiredFields a = new ObjectFieldUpdate2_Test.RequiredFields();
        a.FieldName = 'Name';
        ObjectFieldUpdate2.FiledtoUpdate2('Contact', 1, new List<ObjectFieldUpdate2_Test.RequiredFields>{a});

  • 1
    Did you ever consider to remove the space between "." and RequredField? – Christian Deckert May 5 '15 at 11:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.