You need to sign in to do that
Don't have an account?

Error: Compile Error: Illegal assignment from void to System.HttpResponse
Can somebody help me with this testmethod for an API REST callout.
Here is my callout it, it works just fine when used from a trigger.
public class My_CalloutClass_Stuff {
@future(callout=true)
public static void makeCalloutStuff() {
JSONGenerator jsonObj = JSON.createGenerator(true);
jsonObj.writeStartObject();
jsonObj.writeFieldName('devices');
jsonObj.writeStartArray();
jsonObj.writeStartObject();
jsonObj.writeFieldName('deviceIds');
jsonObj.writeStartArray();
jsonObj.writeStartObject();
jsonObj.writeStringField('id', 'blah');
jsonObj.writeStringField('kind', 'blah');
jsonObj.writeEndObject();
jsonObj.writeEndArray();
jsonObj.writeEndObject();
jsonObj.writeEndArray();
jsonObj.writeEndObject();
String finalJSON = jsonObj.getAsString();
HttpRequest request = new HttpRequest();
String endpoint = 'http://putsreq.com/YRoCBRnRvcUJ8SD2Rhq0';
request.setEndPoint(endpoint);
request.setBody(jsonObj.getAsString());
request.setHeader('Content-Type', 'application/json');
request.setMethod('POST');
String authorizationHeader = 'Bearer ' + 'blah' ;
String VZHeader = 'blah' ;
request.setHeader('VZ-M2M-Token', VZHeader);
request.setHeader('Authorization', authorizationHeader);
// Send the HTTP request and get the response.
HttpResponse response = new HTTP().send(request);
System.debug(response.toString());
System.debug('STATUS:'+response.getStatus());
System.debug('STATUS_CODE:'+response.getStatusCode());
System.debug(response.getBody());
// If the request is successful, parse the JSON response.
if (response.getStatusCode() == 200) {
// Deserialize the JSON string into collections of primitive data types.
Map<String, Object> results = (Map<String, Object>) JSON.deserializeUntyped(response.getBody());
System.debug('=== all keys in the map: ' + results.keySet());
System.debug('=== all values in the map (as a List): ' + results.values());
System.debug('=== all values in the map (as a List): ' + results.size());
// Cast the values in the 'blades' key as a list
List<String> vztoken = new List<String>();
vztoken.addAll(results.keySet());
System.debug('Received the following vztoken info: ' +vztoken);
for (Object Verizon: vztoken) {
System.debug(vztoken);
Verizon__c sbc = new Verizon__c();
sbc.name=String.valueOf(results.values());
sbc.vid__c='id';
sbc.Smartbox__c='id;
sbc.response_code__c=String.valueOf(results.values());
sbc.Action__c='Suspend';
insert sbc;
System.debug('sbc'+sbc);
}
}
}
}
I have a static resource for use with the mocktest
{'sessionresults', 'resultvalue'}
Here is my current, not working testmethod.
@isTest
private class My_CalloutClass_StuffTest {
@isTest static void testPUTCallout() {
// Create the mock response based on a static resource
StaticResourceCalloutMock mock = new StaticResourceCalloutMock();
mock.setStaticResource('GetAnimalResource');
mock.setStatusCode(200);
mock.setHeader('Content-Type', 'application/json;charset=UTF-8');
// Associate the callout with a mock response
Test.setMock(HttpCalloutMock.class, mock);
// Call method to test
HttpResponse resA = My_CalloutClass_Stuff.makeCalloutStuff();
// Verify mock response is not null
System.assertNotEquals(null,resA,
'The callout returned a null response.');
// Verify status code
System.assertEquals(200,resA.getStatusCode(),
'The status code is not 200.');
// Verify content type
System.assertEquals('application/json;charset=UTF-8',
result.getHeader('Content-Type'),
'The content type value is not expected.');
// Verify the array contains 3 items
Map<String, Object> results = (Map<String, Object>)
JSON.deserializeUntyped(resA.getBody());
List<Object> animals = (List<Object>) results.get('animals');
System.assertEquals(3, animals.size(),
'The array should only contain 3 items.');
}
}
Error: Compile Error: Illegal assignment from void to System.HttpResponse at line 13 column 22
Here is my callout it, it works just fine when used from a trigger.
public class My_CalloutClass_Stuff {
@future(callout=true)
public static void makeCalloutStuff() {
JSONGenerator jsonObj = JSON.createGenerator(true);
jsonObj.writeStartObject();
jsonObj.writeFieldName('devices');
jsonObj.writeStartArray();
jsonObj.writeStartObject();
jsonObj.writeFieldName('deviceIds');
jsonObj.writeStartArray();
jsonObj.writeStartObject();
jsonObj.writeStringField('id', 'blah');
jsonObj.writeStringField('kind', 'blah');
jsonObj.writeEndObject();
jsonObj.writeEndArray();
jsonObj.writeEndObject();
jsonObj.writeEndArray();
jsonObj.writeEndObject();
String finalJSON = jsonObj.getAsString();
HttpRequest request = new HttpRequest();
String endpoint = 'http://putsreq.com/YRoCBRnRvcUJ8SD2Rhq0';
request.setEndPoint(endpoint);
request.setBody(jsonObj.getAsString());
request.setHeader('Content-Type', 'application/json');
request.setMethod('POST');
String authorizationHeader = 'Bearer ' + 'blah' ;
String VZHeader = 'blah' ;
request.setHeader('VZ-M2M-Token', VZHeader);
request.setHeader('Authorization', authorizationHeader);
// Send the HTTP request and get the response.
HttpResponse response = new HTTP().send(request);
System.debug(response.toString());
System.debug('STATUS:'+response.getStatus());
System.debug('STATUS_CODE:'+response.getStatusCode());
System.debug(response.getBody());
// If the request is successful, parse the JSON response.
if (response.getStatusCode() == 200) {
// Deserialize the JSON string into collections of primitive data types.
Map<String, Object> results = (Map<String, Object>) JSON.deserializeUntyped(response.getBody());
System.debug('=== all keys in the map: ' + results.keySet());
System.debug('=== all values in the map (as a List): ' + results.values());
System.debug('=== all values in the map (as a List): ' + results.size());
// Cast the values in the 'blades' key as a list
List<String> vztoken = new List<String>();
vztoken.addAll(results.keySet());
System.debug('Received the following vztoken info: ' +vztoken);
for (Object Verizon: vztoken) {
System.debug(vztoken);
Verizon__c sbc = new Verizon__c();
sbc.name=String.valueOf(results.values());
sbc.vid__c='id';
sbc.Smartbox__c='id;
sbc.response_code__c=String.valueOf(results.values());
sbc.Action__c='Suspend';
insert sbc;
System.debug('sbc'+sbc);
}
}
}
}
I have a static resource for use with the mocktest
{'sessionresults', 'resultvalue'}
Here is my current, not working testmethod.
@isTest
private class My_CalloutClass_StuffTest {
@isTest static void testPUTCallout() {
// Create the mock response based on a static resource
StaticResourceCalloutMock mock = new StaticResourceCalloutMock();
mock.setStaticResource('GetAnimalResource');
mock.setStatusCode(200);
mock.setHeader('Content-Type', 'application/json;charset=UTF-8');
// Associate the callout with a mock response
Test.setMock(HttpCalloutMock.class, mock);
// Call method to test
HttpResponse resA = My_CalloutClass_Stuff.makeCalloutStuff();
// Verify mock response is not null
System.assertNotEquals(null,resA,
'The callout returned a null response.');
// Verify status code
System.assertEquals(200,resA.getStatusCode(),
'The status code is not 200.');
// Verify content type
System.assertEquals('application/json;charset=UTF-8',
result.getHeader('Content-Type'),
'The content type value is not expected.');
// Verify the array contains 3 items
Map<String, Object> results = (Map<String, Object>)
JSON.deserializeUntyped(resA.getBody());
List<Object> animals = (List<Object>) results.get('animals');
System.assertEquals(3, animals.size(),
'The array should only contain 3 items.');
}
}
Error: Compile Error: Illegal assignment from void to System.HttpResponse at line 13 column 22
The method makeCalloutStuff() returns void. In the testPUTCallout() test method you are trying to assign the result of that method to an HttpResponse, which won't work as it is void.