public class NotificationTestHelper extends Object implements NotificationObserver
NotificationTestHelper
is used to simplify the testing of
notifications. It is especially useful when notifications are send from
different thread and when testing the direct notification between a viewModel and the View
(via ViewModel.publish(String, Object...)
and ViewModel.subscribe(String, NotificationObserver)
)
This class implements NotificationObserver
and therefore can be added as subscriber. It will record
every received notification and can be tested afterwards.
The ViewModel.publish(String, Object...)
method will send all notifications on the JavaFX UI thread.
Therefore when testing the publishing of notifications JavaFX has to be running which isn't the case
with plain JUnit tests. The NotificationTestHelper
will take care for thread handling.
Example:
public class MyViewModel implements ViewModel {
public static final String ACTION_KEY = "my-action";
public void someAction() {
...
publish(ACTION_KEY);
}
}
// unit test
@Test
public void testSomething() {
MyViewModel viewModel = new MyViewModel();
NotificationTestHelper helper = new NotificationTestHelper();
viewModel.subscribe(MyViewModel.ACTION_KEY, helper);
viewModel.someAction();
assertEquals(1, helper.numberOfReceivedNotifications());
}
You can provide a timeout as constructor parameter.
This is useful in case of asynchronous code (f.e. when notifications are send from another Thread).
By default the timeout is set to 0L. When you have a long running thread
you should use a higher timeout.Modifier and Type | Field and Description |
---|---|
static long |
DEFAULT_TIMEOUT |
Constructor and Description |
---|
NotificationTestHelper()
Create a test helper with a default timeout of 0L millis.
|
NotificationTestHelper(long timeoutInMillis)
Create a test helper with the given timeout in millis.
|
Modifier and Type | Method and Description |
---|---|
int |
numberOfReceivedNotifications() |
int |
numberOfReceivedNotifications(String key) |
void |
receivedNotification(String key,
Object... payload)
Handle the Notification which is passed by the NotificationCenter.
|
public static final long DEFAULT_TIMEOUT
public NotificationTestHelper()
public NotificationTestHelper(long timeoutInMillis)
timeoutInMillis
- the timeout.public void receivedNotification(String key, Object... payload)
NotificationObserver
receivedNotification
in interface NotificationObserver
key
- notification namepayload
- which are passedpublic int numberOfReceivedNotifications()
public int numberOfReceivedNotifications(String key)
key
- the key of the notification.Copyright © 2015 Saxonia Systems AG. All rights reserved.