Class Event


  • @API(status=EXPERIMENTAL,
         since="1.4")
    public class Event
    extends java.lang.Object
    Event represents a single event fired during execution of a test plan on the JUnit Platform.
    Since:
    1.4
    See Also:
    EventType
    • Field Detail

      • timestamp

        private final java.time.Instant timestamp
      • payload

        private final java.lang.Object payload
    • Constructor Detail

      • Event

        private Event​(EventType type,
                      TestDescriptor testDescriptor,
                      java.lang.Object payload)
        Construct an Event with the supplied arguments.
        Parameters:
        type - the type of the event; never null
        testDescriptor - the TestDescriptor associated with the event; never null
        payload - the generic payload associated with the event; may be null
    • Method Detail

      • dynamicTestRegistered

        public static Event dynamicTestRegistered​(TestDescriptor testDescriptor)
        Create an Event for the dynamic registration of the supplied TestDescriptor.
        Parameters:
        testDescriptor - the TestDescriptor associated with the event; never null
        Returns:
        the newly created Event
        See Also:
        EventType.DYNAMIC_TEST_REGISTERED
      • executionSkipped

        public static Event executionSkipped​(TestDescriptor testDescriptor,
                                             java.lang.String reason)
        Create a skipped Event for the supplied TestDescriptor and reason.
        Parameters:
        testDescriptor - the TestDescriptor associated with the event; never null
        reason - the reason the execution was skipped; may be null
        Returns:
        the newly created Event
        See Also:
        EventType.SKIPPED
      • executionStarted

        public static Event executionStarted​(TestDescriptor testDescriptor)
        Create a started Event for the supplied TestDescriptor.
        Parameters:
        testDescriptor - the TestDescriptor associated with the event; never null
        Returns:
        the newly created Event
        See Also:
        EventType.STARTED
      • byPayload

        public static <T> java.util.function.Predicate<Event> byPayload​(java.lang.Class<T> payloadType,
                                                                        java.util.function.Predicate<? super T> payloadPredicate)
        Create a Predicate for events whose payload types match the supplied payloadType and whose payloads match the supplied payloadPredicate.
        Parameters:
        payloadType - the required payload type
        payloadPredicate - a Predicate to match against payloads
        Returns:
        the resulting Predicate
      • byType

        public static java.util.function.Predicate<Event> byType​(EventType type)
        Create a Predicate for events whose event types match the supplied type.
        Parameters:
        type - the type to match against
        Returns:
        the resulting Predicate
      • byTestDescriptor

        public static java.util.function.Predicate<Event> byTestDescriptor​(java.util.function.Predicate<? super TestDescriptor> testDescriptorPredicate)
        Create a Predicate for events whose TestDescriptors match the supplied testDescriptorPredicate.
        Parameters:
        testDescriptorPredicate - a Predicate to match against test descriptors
        Returns:
        the resulting Predicate
      • getType

        public EventType getType()
        Get the type of this Event.
        Returns:
        the event type; never null
        See Also:
        EventType
      • getTestDescriptor

        public TestDescriptor getTestDescriptor()
        Get the TestDescriptor associated with this Event.
        Returns:
        the TestDescriptor; never null
      • getTimestamp

        public java.time.Instant getTimestamp()
        Get the Instant when this Event occurred.
        Returns:
        the Instant when this Event occurred; never null
      • getPayload

        public java.util.Optional<java.lang.Object> getPayload()
        Get the payload, if available.
        Returns:
        an Optional containing the payload; never null but potentially empty
        See Also:
        getPayload(Class), getRequiredPayload(Class)
      • getPayload

        public <T> java.util.Optional<T> getPayload​(java.lang.Class<T> payloadType)
        Get the payload of the expected type, if available.

        This is a convenience method that automatically casts the payload to the expected type. If the payload is not present or is not of the expected type, this method will return Optional.empty().

        Parameters:
        payloadType - the expected payload type; never null
        Returns:
        an Optional containing the payload; never null but potentially empty
        See Also:
        getPayload(), getRequiredPayload(Class)
      • getRequiredPayload

        public <T> T getRequiredPayload​(java.lang.Class<T> payloadType)
                                 throws java.lang.IllegalArgumentException
        Get the payload of the required type.

        This is a convenience method that automatically casts the payload to the required type. If the payload is not present or is not of the expected type, this method will throw an IllegalArgumentException.

        Parameters:
        payloadType - the required payload type; never null
        Returns:
        the payload
        Throws:
        java.lang.IllegalArgumentException - if the payload is of a different type or is not present
        See Also:
        getPayload(), getPayload(Class)
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object