[Salesforce]Agentforce VibesでApexテストクラス自動作成

1 Apexクラス作成する。

public with sharing class Sample {
    public Sample() {

    }
}

2 Agentforce Vibesに指示する。

Sampleのユニットテストクラスを作成して

3 手順2で作成された内容コピーする。

@isTest
public class SampleTest {
    
    @isTest
    static void testSampleConstructor() {
        // Arrange & Act
        Sample sample = new Sample();
        
        // Assert
        System.assertNotEquals(null, sample, 'Sample instance should not be null');
    }
}

4 空のApexテストクラスを作成して、手順3でコピーしたコードを貼り付ける。

[Salesforce]ChatGPDでテストクラス自動作成

ChatGPTを使ったApexテストクラス自動作成

以下のApexテストクラス作成して。
public class AccountProcessor implements Database.Batchable<sObject>,Database.Stateful{ 
 public AccountProcessor(){
 }
 }

以下は、提示された AccountProcessor クラスに対する Apexテストクラス のサンプルです。

@isTest
public class AccountProcessorTest {

    @isTest
    static void testBatchExecution() {
        // テストデータ作成
        List<Account> accounts = new List<Account>();
        for (Integer i = 0; i < 5; i++) {
            accounts.add(new Account(
                Name = 'TestAccount ' + i
            ));
        }
        insert accounts;

        // バッチ実行
        Test.startTest();

        AccountProcessor batch = new AccountProcessor();
        Database.executeBatch(batch, 2); // バッチサイズ指定

        Test.stopTest();

        // 簡易検証(レコードが存在することを確認)
        Integer countAccounts = [SELECT COUNT() FROM Account WHERE Name LIKE 'TestAccount%'];
        System.assertEquals(5, countAccounts);
    }
}

補足

  • Database.Stateful はテストクラス側では特別な対応不要です
  • 実際に executefinish で処理を追加した場合は、その結果を検証する assert を追加してください

もし「バッチの中身(start/execute/finish)も書いてほしい」なら、それも一緒に作れます 👍