Class Spy

java.lang.Object
cloud.opencode.base.test.mock.Spy

public class Spy extends Object
Spy 间谍

Records method invocations for verification.

记录方法调用以供验证。

Features | 主要功能:

  • Method invocation recording - 方法调用记录
  • Call verification support - 调用验证支持

Usage Examples | 使用示例:

Spy spy = new Spy();
spy.record("methodName", args);
assertTrue(spy.wasCalled("methodName"));

Security | 安全性:

  • Thread-safe: Yes (immutable) - 线程安全: 是(不可变)
Since:
JDK 25, opencode-base-test V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Constructor Details

    • Spy

      public Spy()
  • Method Details

    • record

      public void record(String methodName, Object... args)
      Record an invocation 记录一次调用
      Parameters:
      methodName - the method name | 方法名称
      args - the arguments | 参数
    • wasCalled

      public boolean wasCalled(String methodName)
      Verify method was called 验证方法被调用
      Parameters:
      methodName - the method name | 方法名称
      Returns:
      true if called | 如果被调用返回true
    • wasCalledTimes

      public boolean wasCalledTimes(String methodName, int times)
      Verify method was called n times 验证方法被调用n次
      Parameters:
      methodName - the method name | 方法名称
      times - the expected times | 期望次数
      Returns:
      true if called n times | 如果被调用n次返回true
    • getCallCount

      public int getCallCount(String methodName)
      Get call count 获取调用次数
      Parameters:
      methodName - the method name | 方法名称
      Returns:
      the count | 次数
    • getInvocations

      public List<Spy.Invocation> getInvocations()
      Get all invocations 获取所有调用
      Returns:
      the invocations | 调用列表
    • getInvocations

      public List<Spy.Invocation> getInvocations(String methodName)
      Get invocations for method 获取方法的调用
      Parameters:
      methodName - the method name | 方法名称
      Returns:
      the invocations | 调用列表
    • getLastInvocation

      public Spy.Invocation getLastInvocation()
      Get last invocation 获取最后一次调用
      Returns:
      the invocation or null | 调用或null
    • clear

      public void clear()
      Clear recorded invocations 清除记录的调用
    • noInteractions

      public boolean noInteractions()
      Verify no interactions 验证没有交互
      Returns:
      true if no invocations | 如果没有调用返回true