0

I'm still pretty new to this, and I've looked around and haven't been able to find the solution that combines everything like I'm trying to do. I've tried quite a few things, but here's where I stopped once I realized I was heading into an endless knowledge loop:

Main class:

global class SendtoExternalAPI {
    
    @InvocableMethod(label= 'Send values to ExternalAPI' category='Intake__c' callout=true)
    global static void ProcessIntakes(List<ID> intakeIds) {
        List<Intake__c> intakes = [SELECT First_Name__c,  Name__c, Email__c, Street_for_API__c, City_for_API__c, State_for_API__c, Zip_Code_for_API__c, Phone__c, Date_of_Birth_for_API__c  FROM Intake__c WHERE Id in :intakeIds];
    
        String FirstName;
        String LastName;
        String Email;
        String Address1;
        String City;
        String State;
        String Zip;
        String Phone;
        String DOB;
    
        for (Intake__c intake : intakes) {
            FirstName = intake.First_Name__c;
            LastName = intake.Last_Name__c;
            Email = intake.Email__c;
            Address1 = intake.Street_for_API__c;
            City = intake.City_for_API__c;
            State = intake.State_for_API__c;
            Zip = intake.Zip_Code_for_API__c;
            Phone = intake.Phone__c;
            DOB = intake.Date_of_Birth_for_API__c;
            
            String username = 'username';
            String password = 'nicetrySE';
            Blob headerValue = Blob.valueOf(username + ':' + password);
            String authorizationHeader = 'BASIC' + EncodingUtil.base64Encode(headerValue);

            System.debug(FirstName);
            System.debug(LastName);
            System.debug(Email);

            Http http = new Http();
            HttpRequest request = new HttpRequest();
        
            //Please set the endpoint of API gateway which we are going to create in later section
            request.setEndpoint('https://www.ExternalAPI.com');
            request.setMethod('POST');
            request.setHeader('Content-Type', 'application/json');
            request.setHeader('Accept', 'application/json');
            request.setHeader('Authorization', authorizationHeader);

            request.setBody('{"SourceID":"SourceID", "ClientID" : "ClientID", "FirstName":"' + FirstName + '", "LastName":"' + LastName + '", "Address1":"' + Address1 + '" ,"City":"' + City + '","State":"' + State +'","Zip":"'+ Zip + '","Email":"' + Email + '","Phone":"'+ Phone + '","DOB":"'+ DOB + '"}');
            
            HttpResponse response = http.send(request);

            System.debug('response==='+response.getBody());
            System.debug('request==='+request.getBody());

            // Parse the JSON response
            if (response.getStatusCode() != 201) {
                System.debug('The status code returned was not expected: ' +
                response.getStatusCode() + ' ' + response.getStatus());
            } else {
                System.debug(response.getBody());
            }
        }      
    }
}

Mock HTTPCallout API

@isTest global class MockHttpResponseGeneratorAPI implements HttpCalloutMock {
    global HTTPResponse respond(HTTPRequest req) {
        // Optionally, only send a mock response for a specific endpoint
        // and method.
        System.assertEquals('https://www.ExternalAPI.com', req.getEndpoint());
        System.assertEquals('POST', req.getMethod());

        // Create a fake response
        HttpResponse res = new HttpResponse();
        res.setHeader('Content-Type', 'application/json');
        res.setBody('{"First_Name__c":"Random", "Last_Name__c"="Person", "Email__c"="[email protected]",  "Phone__c"="855-867-5309"}');
        res.setStatusCode(200);
        return res;
    }
}

Test Class (promise I had gotten further than this, but my computer restart and undid the changes RIP):

@isTest private class SendExternalAPITest {
    
    @isTest static void testIntake() {   
        Intake__c intake = new Intake__c(First_Name__c='Random', Last_Name__c='Person', Email__c='Email__c"="[email protected]',  Phone__c='855-867-5309');
        insert intake;
        Test.startTest();
        SendtoExternalAPI request = new SendtoExternalAPI(); 
        Test.stopTest();
    }
}
1

1 Answer 1

2

Testing invocable method is no different than testing any Apex method as you can completely ignore the @InvocableMethod annotation from testing point of view.

Here you have created a Http mock class which is supposed to return test response but you are not using that Mock. Mock needs to made active (via Test.setMock()) for your test as follows

@IsTest 
private class SendExternalAPITest {
    
    @IsTest 
    static void testIntake() {   
        Intake__c intake = new Intake__c(First_Name__c='Random', Last_Name__c='Person', Email__c='Email__c"="[email protected]',  Phone__c='855-867-5309');
        insert intake;

        Test.setMock(HttpCalloutMock.class, new MockHttpResponseGeneratorAPI());
        
        Test.startTest();
        
        SendtoExternalAPI request = new SendtoExternalAPI(); 
        
        Test.stopTest();
    }
}

Not the answer you're looking for? Browse other questions tagged .