Salesforce Apex: test that callout hasn't been made

二次信任 提交于 2019-12-12 05:13:12

问题


I want to write a unit test that checks that callout hasn't been made from the trigger.

I know how to test if the callout is made correctly - by implementing HttpCalloutMock:

global class MyHttpCalloutMock implements HttpCalloutMock {
    global HTTPResponse respond(HTTPRequest req) {
        //test HTTPRequest here
    }
}

But if no HTTP request is made, then the respond() method won't be called. So this approach doesn't test if the request was made at all.

I need something like this:

HTTPRequest.assertNoRequestsHaveBeenMade();

How do I do that?


回答1:


So I figured it out. It turns out that Salesforce has methods Test.startTest() and Test.stopTest() that make asynchronous callouts synchronous: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing_tools_start_stop_test.htm

After making callouts synchronous it's much easier to test them.



来源:https://stackoverflow.com/questions/36778106/salesforce-apex-test-that-callout-hasnt-been-made

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!