2

I have implemented several callouts using the Continuation object based on the documentation here. But there is no reference for unit tests. The section in the developer guide only shows how to test with instance methods not with Aura Enabled static methods. I have tried to follow along and found one other discussion on Stack Exchange. But I don't seem to get the correct data into my continuation method. It throws a null pointer exception when I try to get the response status code.

Below is my Continuation and my call back method

 @AuraEnabled(Cacheable=true Continuation=true)
    public static Object resetPassWord(String federationId) {
        URLEndpoints__mdt endpoints = [
                SELECT Url__c,
                        HTTP_Method__c
                FROM URLEndpoints__mdt
                WHERE DeveloperName = :'Okta_Reset'
                LIMIT 1
        ];
        Continuation con = new Continuation(40);
        con.continuationMethod = 'processResetResponse';

        con.state = 'hello world';
        HttpRequest req = new HttpRequest();
        req.setMethod(endpoints.HTTP_Method__c);
        req.setEndpoint(endpoints.Url__c + federationId +
                '/lifecycle/reset_password?sendEmail=false');       
        req.setHeader('Authorization', 'SSWS' + Label.OktaApiToken);
        con.addHttpRequest(req);
        return con;
    }
    @AuraEnabled(Cacheable=true)
    public static Object processResetResponse(List<String> labels, Object state) {
        HttpResponse res = Continuation.getResponse(labels[0]);       
        if (res.getStatusCode() != 200) {
            return res.getBody();
        }
        if (res.getStatusCode() == 200) {
            return res.getBody();
        }
        return null;
    }

Here is my unit test - I was just trying to pass anything into the call back to see if I get get a value. That is why the list of strings and I also tried setting the response.statuscode. But no luck.

 @IsTest
    static void testOktaPasswordReset(){
        List<String>strings = new List<String>{'200'};
        String state = 'State';
        Test.startTest();
        HttpResponse response = new HttpResponse();
        response.setBody('Mock response body');
        response.setStatusCode(200);
        Continuation continuation =(Continuation)PatientOktaController.resetPassWord('123456');
        Test.setContinuationResponse('Continuation-1', response);
        //Map<String, HttpRequest> requestMap = obj.getRequests();
        Test.setContinuationResponse('Continuation -1', response);
        Object result = Test.invokeContinuationMethod(PatientOktaController.processResetResponse(strings, state), continuation);

        Test.stopTest();
    }
1

I heard back from Salesforce about this today. The long and the short is that you can not test Static Continuation Methods. There is a workaround though.

https://developer.salesforce.com/blogs/2020/05/apex-continuations-implementation-and-testing-in-aura-lwc.html.

I will post my code later if I get this working.

2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.