Target - the target class to be mockedEvent - the event type to be collectedpublic class EventCollector<Target,Event>
extends java.lang.Object
Class allows you easily create a mock of the specified type that stubs a single specified method.
When stubbed method is called it's argument got collected into a list. User then can access all collected
values (see getEvents()) or the last collected value (see getLastEvent()).
Mock instance is created from the specified class using Mockito API, so all restrictions are the same.
Method to be stubbed is specified using a BiConsumer which accepts an instance of mocked class and
matcher of the captured event type (note, this matcher will always be null). This consumer
should perform a single call upon the accepted mock, using matcher as an argument - called method will be
stubbed. The most convenient way is to specify a "method link", for example: ActionListener::actionPerformed
Created mock might be accessed by the getTarget() method, and used as a regular instance. Also note
that only specified method got stubbed by the collector, but Mockito API might be used to perform
additional manual configuration upon created target instance.
Additionally collector allows you to register a set of filters, used to decide which event will be collected.
Filters are specified as Predicate of the event type. When stubbed method is called - ALL specified
filters are called to test the captured event. After ALL filters got called - results are checked; if at least
one of the filters returned false - event is ignored, otherwise - it gets collected and processed
by handlers (see EventCollector.EventHandler).
| Modifier and Type | Class and Description |
|---|---|
static interface |
EventCollector.EventHandler<Event>
This interface represents a kind of an event filter that also gets notified when event is collected.
|
| Modifier and Type | Method and Description |
|---|---|
static <T,E> EventCollector<T,E> |
create(java.lang.Class<T> targetClass,
java.util.function.BiConsumer<T,E> method,
java.util.Collection<java.util.function.Predicate<? super E>> filters)
Create an event collector that contains mock instance of the specified target class with a one method stubbed.
|
static <T,E> EventCollector<T,E> |
create(java.lang.Class<T> targetClass,
java.util.function.BiConsumer<T,E> method,
java.util.function.Predicate<? super E>... filters)
Equal to the
create(Class, BiConsumer, Collection) but with vararg for predicate filters. |
static <T,E> EventCollector<T,E> |
create(java.lang.Class<T> targetClass,
EventCombiner<T,E> captor,
java.util.Collection<java.util.function.Predicate<? super E>> filters)
Create collector that will contain a mock of the specified target type,
if will also stub the method represented by the specified
EventCombiner,
and all the events will be filtered by specified predicates. |
static <T,E> EventCollector<T,E> |
create(java.lang.Class<T> targetClass,
EventCombiner<T,E> captor,
java.util.function.Predicate<? super E>... filters)
Equal to the
create(Class, EventCombiner, Collection) but with vararg for predicate filters. |
java.util.List<Event> |
getEvents()
Immutable collection of all the events collected so far.
|
Event |
getLastEvent()
This method either returns the last captured event, or throws an exception if there're no events.
|
Target |
getTarget()
Created and configured mock instance of the specified target type.
|
int |
size()
Number of events collected so far.
|
java.lang.String |
toString() |
@SafeVarargs public static <T,E> EventCollector<T,E> create(java.lang.Class<T> targetClass, java.util.function.BiConsumer<T,E> method, java.util.function.Predicate<? super E>... filters)
create(Class, BiConsumer, Collection) but with vararg for predicate filters.public static <T,E> EventCollector<T,E> create(java.lang.Class<T> targetClass, java.util.function.BiConsumer<T,E> method, java.util.Collection<java.util.function.Predicate<? super E>> filters)
Create an event collector that contains mock instance of the specified target class with a one method stubbed. Method to be stubbed is indicated by the specified predicate. All the predicates in the specified collection will be used to filter out events.
Usability method. Equal to create(Class, EventCombiner, Collection) with EventCombiner
created using the EventCombiner.create(BiConsumer) method.
@SafeVarargs public static <T,E> EventCollector<T,E> create(java.lang.Class<T> targetClass, EventCombiner<T,E> captor, java.util.function.Predicate<? super E>... filters)
create(Class, EventCombiner, Collection) but with vararg for predicate filters.public static <T,E> EventCollector<T,E> create(java.lang.Class<T> targetClass, EventCombiner<T,E> captor, java.util.Collection<java.util.function.Predicate<? super E>> filters)
Create collector that will contain a mock of the specified target type,
if will also stub the method represented by the specified EventCombiner,
and all the events will be filtered by specified predicates.
public Target getTarget()
public int size()
public java.util.List<Event> getEvents()
public Event getLastEvent()
java.lang.IllegalStateException - if there're no events capturedpublic java.lang.String toString()
toString in class java.lang.Object