function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Vishwas SVishwas S 

Illegal assignment from void to System.HttpResponse

Hi all,

I am writing a Test Class for Apex REST Callout which is throwing an error as "Illegal assignment from void to System.HttpResponse" .

In the test class error is in this line

 HttpResponse response = Account6Class.postmethod(aId); 
 
Please help to reslove this error. Thanks in advance.

This is code of Trigger:
trigger Account6 on Account (after insert) {
        if (Trigger.isInsert) {
        Set<Id> accountIdSet = new Set<Id>();
        for(Account accObj: trigger.new){
            accountIdSet.add(accObj.id);
         }
        if(!accountIdSet.isEmpty()){
            Account6Class.postmethod(accountIdSet);
        }
    }

This is code of Class:
public class Account6Class {
@future(callout=true)
    public static void postmethod(Set<Id> accountIdset) {       
        string resultBodyGet = '';
        list<Account> accts = [SELECT  Name, Id, Description, Phone FROM Account WHERE Id IN:accountIdset];
       for(Account c : accts){         
            MAp<String, String> tags = new Map<String, String>();
            tags.put('id', c.Id);
            tags.put('Name', c.Name);
            tags.put('Description',c.Description);
            tags.put('Phone',c.Phone);               
            try{
                string endpoint = 'https://abc.com/xyz';
                HttpRequest req = new HttpRequest();
                req.setEndpoint(endpoint);
                req.setMethod('POST');
                req.setHeader('Content-type', 'application/json');
                req.setbody(JSON.serialize(tags));
                Http http = new Http();
                HTTPResponse response = http.send(req); 
                resultBodyGet = response.getBody();
                system.debug('Output response:' + resultBodyGet);
                accResponse myAccResponse = new accResponse();
                myAccResponse = (accResponse) JSON.deserialize(resultBodyGet, accResponse.class);
                system.debug('#### myAccResponse: ' + myAccResponse);
            }
            catch (exception e) {                              
            }   
        }
    }    
public class accResponse {
        public string message {get;set;}
    }
}

This is code of  HttpCallouMock Class:
@isTest
global class Account6Test implements HttpCalloutMock{
    global HTTPResponse respond(HTTPRequest request) {
         HttpResponse response = new HttpResponse();
        response.setHeader('Content-Type', 'application/json');
        response.setBody('{"Account": ["Test6"]}');
        response.setStatusCode(200);
        return response; 
    }
}

This is code of  Test Class:
@isTest
private class Account6Test1 {
    @isTest static void testPostCallout() {
        Test.setMock(HttpCalloutMock.class, new Account6Test() );
        Set<Id> aId = new Set<id>();
         Account test = new Account();
            test.Name = 'Test6';
            test.Description = 'FooDes';
            test.Phone = '1231231232';
            insert test;
            aId.add(test.Id);
          System.debug('Accounts are ' + aId);
        HttpResponse response = Account6Class.postmethod(aId);
        String contentType = response.getHeader('Content-Type');
        System.assert(contentType == 'application/json');
        String actualValue = response.getBody();
        System.debug(response.getBody());
        String expectedValue = '{"Account": ["Test6"]}';
        System.assertEquals(actualValue, expectedValue);
        System.assertEquals(200, response.getStatusCode());
        
    }
 }

Sachin HoodaSachin Hooda
It's because, postMethod returns nothing and you're assigning that value to an instance of HttpResponse.
You only need to change return type of postMethod of Account6class to HttpResponse & return response. This should fix your issue.
Vishwas SVishwas S

Hi Sachin Hooda thanks for reply.

If I change the return type of class i am getting the error as "Future methods do not support return type of System.HttpResponse".
Please help to reslove this error. 

Sachin HoodaSachin Hooda
You need to use HttpCalloutMock. So you'll be able to create the fake responses and will be able to test under different responses. Refer here. (https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_restful_http_testing_httpcalloutmock.htm)