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
magandrezmagandrez 

Error deploying a HttpCalloutMock out-of-the-box implementation.

Hi all,

 

I developed a functionality that makes use of HttpRequest class. In order to test it I used HttpCalloutMock. This functionality is withing a @future (callout=true) method. I developed the test classes following the documentation for HttpCalloutMock. The tests (2) pass in sandbox, but when deploying to production they both give error System.NullPointerException (apparently the HttpResponse is null) in the assertion line. Here is the code for the tests and the implementations for HttpCalloutMock within the same Test class:

 

@isTest
global class TrustCalloutTest {
    
    global class TrustCalloutMockBasicCallout implements HttpCalloutMock {
        
        global HTTPResponse respond(HTTPRequest req) {
            
            HttpResponse res = new HttpResponse();
            res.setBody('TEST');
            res.setStatusCode(200);
            return res;
        }
    }
    
    global class TrustCalloutMockRequestKey implements HttpCalloutMock {
        
        global HTTPResponse respond(HTTPRequest req) {
            
            HttpResponse res = new HttpResponse();
            res.setBody('RECEIVED');
            res.setStatusCode(200);
            return res;
        }
    }
    
    static testmethod void testCalloutRequireKey() {
    	
        HttpResponse res; 
        Test.setMock(HttpCalloutMock.class, new TrustCalloutMockRequestKey());
        res = TrustCallout.requestTransferKey('BLAH','https://beta2.trustpoint.fi/API/requirekey.php');
        System.assertEquals(200, res.getStatusCode());
    }
    static testmethod void testCalloutBasicCallout(){
         
    	HttpResponse res;
        Test.setMock(HttpCalloutMock.class, new TrustCalloutMockBasicCallout()); 
        res = TrustCallout.basicCallout('BLAH','https://beta2.trustpoint.fi/API/committransfer.php');
        System.assertEquals(200, res.getStatusCode());    
    }
}

 

The actual callout works normally, and it also follows the documentation.

 

Endpoints were added at both sides (production and sandbox).

 

Any idea of what is going on?

 

Thanks.

 

MGA.

 

 

Best Answer chosen by Admin (Salesforce Developers) 
__

Sorry, I should have read the whole thing :) you clearly state that you added the points to both org ...

 

Try using Test.StartTest() and Test.StopTest() since @future is async process, you need to wait until after Test.StopTest to do assertions... 

All Answers

__

is your request url added to authorized end points in production? 

magandrezmagandrez

Hi Oleg,

 

I added the authorized end points but the validation fails in the same points the same way, System.NullPointerException.

__

Sorry, I should have read the whole thing :) you clearly state that you added the points to both org ...

 

Try using Test.StartTest() and Test.StopTest() since @future is async process, you need to wait until after Test.StopTest to do assertions... 

This was selected as the best answer
magandrezmagandrez
Thanks Oleg. The problem was a combination of not adding the endpoints and not adding the test.StartTest() test.StopTest().