Apex @testSetupアノテーションの使い方のサンプル

環境
salesforce

構文
@TestSetup アノテーションで定義されたメソッドは、クラスのすべてのテストメソッドで使用できる一般的なテストレコードの作成に使用されます。
書式
@TestSetup static void methodName() {
コード
}

サンプルコード

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
@isTest
private class SamTestClass{
@testSetup
static void setup() {
Account acc = new Account(Name = 'Sample Account');
insert acc;
}
@isTest
static void testmethod1() {
// SOQLでレコードを取得する
Account acc = [SELECT Id, Name FROM Account];
}
}
@isTest private class SamTestClass{ @testSetup static void setup() { Account acc = new Account(Name = 'Sample Account'); insert acc; } @isTest static void testmethod1() { // SOQLでレコードを取得する Account acc = [SELECT Id, Name FROM Account]; } }
@isTest
private class SamTestClass{
    @testSetup
    static void setup() {
        Account acc = new Account(Name = 'Sample Account');
        insert acc;
    }

    @isTest
    static void testmethod1() {
        // SOQLでレコードを取得する
        Account acc = [SELECT Id, Name FROM Account];
    }
}

 

Apex

Posted by arkgame