Class InMemoryInvocationEventEmitter

java.lang.Object
com.fayupable.logged.test.InMemoryInvocationEventEmitter
All Implemented Interfaces:
InvocationEventEmitter

public final class InMemoryInvocationEventEmitter extends Object implements InvocationEventEmitter
An InvocationEventEmitter that records every event it receives in memory instead of publishing it anywhere, so a test can assert on what @Logged actually observed about a call.

Registering this as the InvocationEventEmitter bean (or passing it directly to a framework-free LoggedAspect) turns the emission side effect into something a test can inspect synchronously, without capturing log output or standing up a real sink.

Backed by a CopyOnWriteArrayList because a @Logged method under test may itself dispatch work to another thread (for example through FlowContextPropagatingExecutor); emission from more than one thread must not corrupt the recorded list or lose an event.

  • Constructor Details

    • InMemoryInvocationEventEmitter

      public InMemoryInvocationEventEmitter()
  • Method Details

    • emit

      public void emit(MethodInvocationEvent event)
      Description copied from interface: InvocationEventEmitter
      Publishes the given invocation event to whatever sink this implementation targets.

      Implementations must not throw checked or unchecked exceptions that would propagate back into the intercepted method call. Emission failures (for example a logging backend being unavailable) should be handled internally by the implementation.

      Specified by:
      emit in interface InvocationEventEmitter
      Parameters:
      event - the invocation event to publish, never null
    • events

      public List<MethodInvocationEvent> events()
      Returns every event recorded so far, in emission order.
      Returns:
      an immutable snapshot of the recorded events
    • count

      public int count()
      Returns the number of events recorded so far.
      Returns:
      the recorded event count
    • lastEvent

      public MethodInvocationEvent lastEvent()
      Returns the most recently recorded event.
      Returns:
      the last recorded event
      Throws:
      IllegalStateException - if no event has been recorded yet
    • clear

      public void clear()
      Discards every event recorded so far, so this instance can be reused across test methods without leaking state between them.