LoginSignup
0
0

More than 5 years have passed since last update.

SalesforceのUnitTestで出た謎の [Internal Salesforce Error] の解消

Posted at

追えないエラー

テスト流したらこんな結果がとれた。
(ant migrationツールで実施した場合のログ)

Test Failures:
 1.  Callout_Test.httpRequest -- Internal Salesforce Error: 182815349-17184 (-1472235887) (-1472235887)
     Stack trace: null

情報がどのテスト流したらエラーが起きた、くらいしかない :innocent:

結論から

コールアウトのクライアント証明書にスペースが入ってると出る。

解説

クライアント証明書(参照: HTTP 要求での証明書の使用 )を使ってコールアウトを実施しているコードがあったとする。

setClientCertificateNameに渡す文字列にスペースが含まれていると出た。(2018/12/13 現在)

    final HttpRequest request = new HttpRequest();
    request.setMethod('GET');

    request.setEndPoint(endpoint);
    request.setHeader('Content-Type', 'application/json; charset=UTF-8');

    String clientCertificateName;

    if(Test.isRunningTest()) {
        // clientCertificateName = 'test dummy certificate name'; // NG
        clientCertificateName = 'dummyClientCertName'; // OK 
    } else {
        // 何かしら設定から取ってきた方が良い
        clientCertificateName = 'productionClientCertName';
    }
    request.setClientCertificateName(clientCertificateName);

    final Http h = new Http();
    final HttpResponse response = h.send(request);

どうしてもカバレッジ通したい時などに使う。
Test側では証明書設定が無いことで起きる例外をtry-catchすることになるけど、それでカバレッジは通る。

※実際は Test.isRunningTest じゃなくて、他の方法でTest側から注入出来るような機構があったほうがよい

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0