You need to sign in to do that
Don't have an account?
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());
}
}
You only need to change return type of postMethod of Account6class to HttpResponse & return response. This should fix your issue.
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.