+ Start a Discussion
Christopher PezzaChristopher Pezza 

Testing Metadata and Web Services

Hi, I am trying to test one of my Apex Methods but get a Cannot test web service callouts and am counfused out how to implement the test script to handle this. 

Here is my Apex Method im trying to test.
public PageReference initialiveFields() {
        List<Fields__c> newListFields = new List<Fields__c>();
        Master_Workbook__c selectObj = [SELECT Id, Object_API_Name__c FROM Master_Workbook__c WHERE Id =: selectedObject];

		Master_Workbook_Settings__mdt doNotCreateFields = queryMasterWorkbookSettings(DONOTCREATEFIELDCATEGORY);
		Master_Workbook_Settings__mdt objectBasedFields = queryMasterWorkbookSettings(selectObj.Object_API_Name__c);

        List<Fields__c> objExistingFields = [SELECT Field_API_Name__c FROM Fields__c WHERE Object_Name__c =: selectObj.Id];
        Map<String, Schema.SObjectField> fields = schemaMap.get(selectObj.Object_API_Name__c).getDescribe().fields.getMap();
        for (SObjectField sobField : fields.values()) {
            DescribeFieldResult sobFieldDescribe = sobField.getDescribe();
            String metadataHere = selectObj.Object_API_Name__c + PERIOD + sobFieldDescribe.getName();
            if (testCreation(sobFieldDescribe, doNotCreateFields, objectBasedFields, objExistingFields)) {
                MetadataServices.CustomField customField = (MetadataServices.CustomField) service.readMetadata(CUSTOMFIELDTEXT, new String[] { metadataHere }).getRecords()[0];
                newListFields.add(generateFieldRecord(sobFieldDescribe, selectObj.Id, customField.description, customField.fullName, objectBasedFields));
        insert newListFields;

        selectObj.IsInitialized__c = true;
        update selectObj;

        return start();

Any constructive guidance would be great, Kinda confused on how to use Salesforce Test webservice callout https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_callouts_wsdl2apex_testing.htm
There don't appear to be any actual callouts in your code above.  There are Custom Metadata which you can read about testing here [1] and field describes which are not callouts.  The only thing that I can see is that hte MetadataServices class may make a callout.  In that case you will need generate your mock class and set the Test.setMock but you will be returning the data for the expected Metadata describe.

[1] https://developer.salesforce.com/blogs/engineering/2015/05/testing-custom-metadata-types.html