MessageTrap implements Subscriber { public static <N extends Notification> void verifyNotificationNotBroadcast(Class<N> notificationClass, Runnable action, EventSchedulerType schedulerType) { MessageTrap<N> messageTrap = MessageTrap.createAndSubscribe(notificationClass, schedulerType); action.run(); messageTrap.getCapture().ifPresent(n -> { throw new AssertionError("Unexpected notification of type [" + notificationClass.getName() + "] broadcast: " + n); }); } private  MessageTrap(Class<T> type, EventSchedulerType schedulerType); static MessageTrap<T> createAndSubscribe(Class<T> type, EventSchedulerType schedulerType); @Subscribe void anyNotificationOfType(T n); Optional<T> getCapture(); void reset(); @Override EventSchedulerType getSchedulerType(); static void verifyNotificationNotBroadcast(Class<N> notificationClass, Runnable action, EventSchedulerType schedulerType);  }
@Test public void verifyNotificationNotBroadcast_noBroadcast() { MessageTrap.verifyNotificationNotBroadcast(TestNotification.class, Runnables::doNothing, TestSchedulerType.TEST_SCHEDULER_TYPE); }
@Test public void verifyNotificationNotBroadcast_withBroadcast() { Assertions.assertThrows( AssertionError.class, () -> MessageTrap.verifyNotificationNotBroadcast(TestNotification.class, () -> NotificationRouter.get().broadcast(new TestNotification()), TestSchedulerType.TEST_SCHEDULER_TYPE)); }