Apexテストメソッドでトリガを無効にするには、isRunningTest()を使用します。これにより、「SOQLクエリが多すぎます: 101」エラーを防ぎ、テストケースが失敗しないようにします。isRunningTest()は、現在のコードがテストメソッドから呼び出されたかどうかを判定し、適切な動作を実行します。例えば、バッチ実行やコールアウトのテスト時に有効です。これにより、テスト環境での特定の動作を制御できます。
isRunningTest() を利用してトリガを設定します。isRunningTest() - 現在実行されているコードがテストメソッドに含まれているコードから呼び出された場合は true を、それ以外の場合は false を返します。このメソッドは、それがテストから呼び出されたかどうかによって異なるコードを実行する必要がある場合に使用します。
Test.isRunningTest コードの例
if(Test.isRunningTest()){
//何かを実行する
}
その他の使用シナリオ
1.Test.IsRunningTest() が true である場合はトリガによってバッチが実行されないようにしてから、その独自のテストメソッドを使用してバッチクラスをテストします。
2.コールアウトのテスト - コールアウトコードで Test.isRunningTest() をチェックしてユニットテストのコンテキスト内で実行されているかどうかを確認し、HttpResponse.send() 要求からコールアウト応答を取得するのではなく、代わりに事前作成済みのテスト文字列を返します。
3.System.purgeOldAsyncJobs このコンテキスト内で呼び出された場合は、パージされるデータによっては実行時間が長くなる場合があります。ここで、Test.isRunningTest() の使用が役立ちます。
000386600

We use three kinds of cookies on our websites: required, functional, and advertising. You can choose whether functional and advertising cookies apply. Click on the different cookie categories to find out more about each category and to change the default settings.
Privacy Statement
Required cookies are necessary for basic website functionality. Some examples include: session cookies needed to transmit the website, authentication cookies, and security cookies.
Functional cookies enhance functions, performance, and services on the website. Some examples include: cookies used to analyze site traffic, cookies used for market research, and cookies used to display advertising that is not directed to a particular individual.
Advertising cookies track activity across websites in order to understand a viewer’s interests, and direct them specific marketing. Some examples include: cookies used for remarketing, or interest-based advertising.