Unit testing listener behaviour where the time between events is important is a pain because it is impossible to specify the time of the event.

I don't think there is a way to expose this behaviour in only test code, but I can do so if someone points out how.

Lastly, if the constructor is intentionally left out, it might be good to document this.

Comment From: sbrannen

Unit testing listener behaviour where the time between events is important is a pain because it is impossible to specify the time of the event.

Do you need to manually specify the timestamps?

Or are you looking for a way to consume events fired during the execution of a test in the order in which they were fired?

If the latter applies to you, you may find the ApplicationEvents testing feature useful.

Comment From: sbrannen

Do you need to manually specify the timestamps?

As an alternative, would introducing a constructor that accepts a java.time.Clock meet your needs?

Comment From: medwards

recording application events published in the ApplicationContext so that assertions can be performed against those events within tests

I don't need to check for published events in this unit test (I mock the ApplicationEventPublisher provided to the component)

The component I want to test reacts differently based on the amount of time between events that it listens to (using the event timestamp to determine this).

Clock works for me, I can mock it easily. I've updated the branch /w this alternative.

Comment From: medwards

@sbrannen : anything else I can provide in order to triage?

Comment From: sbrannen

@sbrannen : anything else I can provide in order to triage?

Thanks for the ping.

I'm in favor of the change, since I'm generally interested in making things easier to test.

So, I'll bring it up within the team to decide if we want to introduce this new constructor.

Comment From: sbrannen

Related to #24454, #24884, #25782, and #26542.

Comment From: medwards

Fixed whitespace.

Comment From: sbrannen

This has been merged into main.

Thanks for submitting your first contribution to the Spring Framework! 👍