Class SnapshotAssert

java.lang.Object
cloud.opencode.base.test.assertion.SnapshotAssert

public final class SnapshotAssert extends Object
Snapshot Assert - JSON snapshot assertion utility 快照断言 - JSON快照断言工具

Compares actual JSON strings against stored snapshot files. On first run (when the snapshot file does not exist), the snapshot is created automatically. On subsequent runs, the actual value is compared against the stored snapshot.

将实际JSON字符串与存储的快照文件进行比较。首次运行(快照文件不存在时), 会自动创建快照。后续运行时,将实际值与存储的快照进行比较。

Features | 主要功能:

  • Automatic snapshot creation on first run - 首次运行自动创建快照
  • Whitespace-normalized comparison - 空白规范化比较
  • Custom snapshot directory support - 自定义快照目录支持
  • Snapshot update via system property - 通过系统属性更新快照

Usage Examples | 使用示例:

// Compare against snapshot in default directory (src/test/resources/snapshots)
SnapshotAssert.assertMatchesSnapshot("user-response", actualJson);

// Compare against snapshot in custom directory
SnapshotAssert.assertMatchesSnapshot(
    Path.of("src/test/resources/my-snapshots"),
    "user-response",
    actualJson
);

// Force update snapshots by setting system property:
// -Dopencode.test.update-snapshots=true

Security | 安全性:

  • Thread-safe: Yes (stateless utility) - 线程安全: 是(无状态工具类)
  • File I/O: Uses UTF-8 encoding - 文件I/O: 使用UTF-8编码
Since:
JDK 25, opencode-base-test V1.0.3
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Details

    • assertMatchesSnapshot

      public static void assertMatchesSnapshot(String snapshotName, String actualJson)
      Asserts that the actual JSON matches the stored snapshot. Uses the default snapshot directory (src/test/resources/snapshots). 断言实际JSON匹配存储的快照。使用默认快照目录。
      Parameters:
      snapshotName - the snapshot name (without extension) | 快照名称(不含扩展名)
      actualJson - the actual JSON string | 实际JSON字符串
      Throws:
      AssertionException - if the actual JSON does not match the snapshot | 如果实际JSON不匹配快照
      TestException - if an I/O error occurs | 如果发生I/O错误
    • assertMatchesSnapshot

      public static void assertMatchesSnapshot(Path snapshotDir, String snapshotName, String actualJson)
      Asserts that the actual JSON matches the stored snapshot in the specified directory. 断言实际JSON匹配指定目录中存储的快照。
      Parameters:
      snapshotDir - the snapshot directory | 快照目录
      snapshotName - the snapshot name (without extension) | 快照名称(不含扩展名)
      actualJson - the actual JSON string | 实际JSON字符串
      Throws:
      AssertionException - if the actual JSON does not match the snapshot | 如果实际JSON不匹配快照
      TestException - if an I/O error occurs | 如果发生I/O错误
    • deleteSnapshot

      public static boolean deleteSnapshot(String snapshotName)
      Deletes the snapshot file if it exists. 删除快照文件(如果存在)。
      Parameters:
      snapshotName - the snapshot name (without extension) | 快照名称(不含扩展名)
      Returns:
      true if the file was deleted, false if it did not exist | 如果文件被删除返回true,如果不存在返回false
    • deleteSnapshot

      public static boolean deleteSnapshot(Path snapshotDir, String snapshotName)
      Deletes the snapshot file if it exists. 删除快照文件(如果存在)。
      Parameters:
      snapshotDir - the snapshot directory | 快照目录
      snapshotName - the snapshot name (without extension) | 快照名称(不含扩展名)
      Returns:
      true if the file was deleted, false if it did not exist | 如果文件被删除返回true,如果不存在返回false