Class Event


  • @API(status=MAINTAINED,
         since="1.7")
    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 Summary

      Fields 
      Modifier and Type Field Description
      private java.lang.Object payload  
      private org.junit.platform.engine.TestDescriptor testDescriptor  
      private java.time.Instant timestamp  
      private EventType type  
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private Event​(EventType type, org.junit.platform.engine.TestDescriptor testDescriptor, java.lang.Object payload)
      Construct an Event with the supplied arguments.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      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.
      static java.util.function.Predicate<Event> byTestDescriptor​(java.util.function.Predicate<? super org.junit.platform.engine.TestDescriptor> testDescriptorPredicate)
      Create a Predicate for events whose TestDescriptors match the supplied testDescriptorPredicate.
      static java.util.function.Predicate<Event> byType​(EventType type)
      Create a Predicate for events whose event types match the supplied type.
      static Event dynamicTestRegistered​(org.junit.platform.engine.TestDescriptor testDescriptor)
      Create an Event for the dynamic registration of the supplied TestDescriptor.
      static Event executionFinished​(org.junit.platform.engine.TestDescriptor testDescriptor, org.junit.platform.engine.TestExecutionResult result)
      Create a finished Event for the supplied TestDescriptor and TestExecutionResult.
      static Event executionSkipped​(org.junit.platform.engine.TestDescriptor testDescriptor, java.lang.String reason)
      Create a skipped Event for the supplied TestDescriptor and reason.
      static Event executionStarted​(org.junit.platform.engine.TestDescriptor testDescriptor)
      Create a started Event for the supplied TestDescriptor.
      static Event fileEntryPublished​(org.junit.platform.engine.TestDescriptor testDescriptor, org.junit.platform.engine.reporting.FileEntry file)
      Create an Event for a published file for the supplied TestDescriptor and FileEntry.
      java.util.Optional<java.lang.Object> getPayload()
      Get the payload, if available.
      <T> java.util.Optional<T> getPayload​(java.lang.Class<T> payloadType)
      Get the payload of the expected type, if available.
      <T> T getRequiredPayload​(java.lang.Class<T> payloadType)
      Get the payload of the required type.
      org.junit.platform.engine.TestDescriptor getTestDescriptor()
      Get the TestDescriptor associated with this Event.
      java.time.Instant getTimestamp()
      Get the Instant when this Event occurred.
      EventType getType()
      Get the type of this Event.
      static Event reportingEntryPublished​(org.junit.platform.engine.TestDescriptor testDescriptor, org.junit.platform.engine.reporting.ReportEntry entry)
      Create an Event for a reporting entry published for the supplied TestDescriptor and ReportEntry.
      java.lang.String toString()  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Field Detail

      • timestamp

        private final java.time.Instant timestamp
      • testDescriptor

        private final org.junit.platform.engine.TestDescriptor testDescriptor
      • payload

        private final java.lang.Object payload
    • Constructor Detail

      • Event

        private Event​(EventType type,
                      org.junit.platform.engine.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

      • reportingEntryPublished

        public static Event reportingEntryPublished​(org.junit.platform.engine.TestDescriptor testDescriptor,
                                                    org.junit.platform.engine.reporting.ReportEntry entry)
        Create an Event for a reporting entry published for the supplied TestDescriptor and ReportEntry.
        Parameters:
        testDescriptor - the TestDescriptor associated with the event; never null
        entry - the ReportEntry that was published; never null
        Returns:
        the newly created Event
        See Also:
        EventType.REPORTING_ENTRY_PUBLISHED
      • fileEntryPublished

        @API(status=MAINTAINED,
             since="1.13.3")
        public static Event fileEntryPublished​(org.junit.platform.engine.TestDescriptor testDescriptor,
                                               org.junit.platform.engine.reporting.FileEntry file)
        Create an Event for a published file for the supplied TestDescriptor and FileEntry.
        Parameters:
        testDescriptor - the TestDescriptor associated with the event; never null
        file - the FileEntry that was published; never null
        Returns:
        the newly created Event
        Since:
        1.12
        See Also:
        EventType.FILE_ENTRY_PUBLISHED
      • dynamicTestRegistered

        public static Event dynamicTestRegistered​(org.junit.platform.engine.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​(org.junit.platform.engine.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​(org.junit.platform.engine.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
      • executionFinished

        public static Event executionFinished​(org.junit.platform.engine.TestDescriptor testDescriptor,
                                              org.junit.platform.engine.TestExecutionResult result)
        Create a finished Event for the supplied TestDescriptor and TestExecutionResult.
        Parameters:
        testDescriptor - the TestDescriptor associated with the event; never null
        result - the TestExecutionResult for the supplied TestDescriptor; never null
        Returns:
        the newly created Event
        See Also:
        EventType.FINISHED
      • 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 org.junit.platform.engine.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 org.junit.platform.engine.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