3

Rest class :

@RestResource(urlMapping='/uhb/sdefs/')

global with sharing class GSSRestResourceChatter{

    @HttpPatch
    global static String doPost(){
        List<ChatterDTO> fieldParsers = (List<ChatterDTO>)JSON.deserialize(
            RestContext.request.requestBody.tostring(), 
            List<ChatterDTO>.class);    

        System.debug('REST Context>>>>>>>>>>>'+RestContext.request.requestBody.tostring());
        System.debug('fieldParsers>>>>>>>>>>>'+fieldParsers);


     // do something   


    }

    global class ChatterDTO{
        public String ParentId{ get; set; }
        public String Body { get; set; }

    }  
}

Test class:

@isTest 
private class Test_GSSRestResourceChatter {
static testmethod void testGSSRestResourceChatter(){

 Account a = new Account(Name='Tester', 
        AccountNumber='101552',
        Account_Group__c = 'Sold To Party'
        );
        insert a;

System.debug('accountid>>>>>' +a.Id);
   Test.startTest();  

        RestRequest req = new RestRequest(); 
        RestResponse res = new RestResponse();
        String jsonm ='[{"Some String"}]';
        req.requestURI = 'https://ap2.salesforce.com/services/apexrest/uhb/sdefs/'; 
        req.httpMethod = 'POST';
        req.requestBody = Blob.valueof(jsonm);
        RestContext.request = req;
        RestContext.response = res;    
        GSSRestResourceChatter.doPost(); 
        GSSRestResourceChatter.ChatterDTO thisChatter = GSSRestResourceChatter.doPost();
       // System.assert(thisChatter!=null);
      System.assertEquals(thisChatter.ParentId, String.valueOf(a.Id));


     Test.stopTest();

}
}

I am getting error while saving the test class as

Compile Error: Illegal assignment from String to GSSRestResourceChatter.ChatterDTO at line 23 column 43

Please help me to sort out the error

  • What is actually returning the doPost() method? A string containing some sort of JSON serialization of a ChatterDTO object? If not you can assign ChatterDTO with a Object.valueOf of a String value – Enrico Murru Dec 31 '15 at 10:07
  • doPost() method returns String . Yes serialization has been done for ChatterDTO object @Enreeco – DOMINIC EDWARD Dec 31 '15 at 10:14
  • Now on line 23 use "GSSRestResourceChatter.ChatterDTO thisChatter = (GSSRestResourceChatter.ChatterDTO)JSON.deserialize(GSSRestResourceChatter.doPost()); ... this should work! – Enrico Murru Dec 31 '15 at 10:16
  • JSON.deserialize is done in doPost method. – DOMINIC EDWARD Dec 31 '15 at 10:22
  • The doPost() method returns a String and not a ChatterDTO object – Enrico Murru Dec 31 '15 at 10:23
0

The problem was the fact that the doPost() method is not returning a ChatterDTO object

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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