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
Will BoyceWill Boyce 

Help with a callout test class for a method with parameters from a trigger

Hello community!  I have a REST Post callout that is fired from a trigger.  I am passing three parameters from that trigger into the callout method.  I am having trouble writing a text class for this callout.  Any help or suggestions are welcomed.  Thanks in advance for your time and knowledge!

HERE IS THE CLASS:

public class RESTCallout {
    @future (callout=true)
    public static void makePostCallout(string taskType, string orderNumber, string siteId) {
       
        Http http = new Http();
        HttpRequest request = new HttpRequest();
        String authorizationHeader = 'BASIC' + 'somecredential';
        String endpoint = 'http://api.website.com/api2/rest/v1/order/'+orderNumber+'/communication/'+siteId;
        System.debug(endpoint);
        
        request.setEndpoint(endpoint);
        request.setMethod('POST');
        request.setHeader('Content-Type', 'application/json;charset=UTF-8');
        request.setHeader('Authorization', authorizationHeader);
        
        JSONGenerator gen = JSON.createGenerator(true);    
        gen.writeStartObject();      
        gen.writeStringField('communicationType', taskType);
        gen.writeEndObject();    
        String jsonS = gen.getAsString();
        System.debug('jsonMaterials'+jsonS);
        
        request.setBody(jsonS);
        HttpResponse response = http.send(request);
       
        if (response.getStatusCode() != 200) {
            System.debug('The status code returned was not expected: ' +
            response.getStatusCode() + ' ' + response.getStatus());
        } else {
            System.debug(response.getBody());
        }
    }
}

HERE IS THE TRIGGER:

trigger PostToFulfillment on Task (after update, after insert) {
    
        Set<Id> caseIdSet = new Set<Id>();

    for(Task t: Trigger.new) {
        If(t.whatId != null && t.whatId.getsObjectType() == Case.sObjectType ){
            caseIdSet.add(t.whatId);
        }       
    }

    if(!caseIdSet.isEmpty()) {
        Map<Id, Case> caseMap = new Map<Id, Case>([Select Id, RecordType.DeveloperName, RecordTypeId, Order_Number__c, Type, Site_ID__c, Status from Case where Id =: caseIdSet]);
        for(Task t: Trigger.new) {
            If(t.whatId.getsObjectType() == Case.sObjectType){
                Case c = caseMap.get(t.whatId);
                if(c != null && t.Status == 'Completed' && c.RecordType.DeveloperName == 'eConnect') {
                    eConnTaskRESTCallout.makePostCallout(t.Type, c.Order_Number__c, c.Site_ID__c);
                }
            }
        }
    }    
}
Will BoyceWill Boyce
I get this error for the code below: Illegal assignment from void to System.HttpResponse

here is my mock class:

@isTest
global class RESTCalloutMock implements HttpCalloutMock {
    global HTTPResponse respond(HTTPRequest request) {
        HttpResponse response = new HttpResponse();
        response.setHeader('Content-Type', 'application/json');
        response.setBody('{ "communicationType" : "Phone"}');
        response.setstatusCode(200);
        return response;
    }
}

here is my attempt to make a test class for the callout:

@isTest
private class RESTCalloutTest {

    @isTest static void testPostCallout() {
        String taskType = 'Call';
        String orderNumber = 'P1234567';
        String siteId = '987654';
        
        Test.setMock(HttpCalloutMock.class, new RESTCalloutMock());
        HttpResponse response = RESTCallout.makePostCallout(taskType, orderNumber, siteId);
        
        String contentType = response.getHeader('Content-Type');
        system.assert(contentType == 'application/json');
        String actualValue = response.getBody();
        System.debug(response.getbody());
        String expectedValue = '{ "communicationType" : "Phone"}';
        System.assertEquals(actualValue, expectedValue);
        System.assertEquals(200, response.getStatusCode());
    }
}
Rakesh BachhavRakesh Bachhav
Hey Guyz did you get any resolution stuck in same problem today? Please ans.