@Test public void all_filters_are_called() { // given: Predicate<Integer> filter = Mockito.mock(UberClasses.cast(Predicate.class)); EventHandler<Integer> filterEH = Mockito.mock(UberClasses.cast(EventHandler.class)); Mockito.when(filter.test(100)).thenReturn(true); Mockito.when(filterEH.test(100)).thenReturn(true); Mockito.when(filter.test(200)).thenReturn(false); Mockito.when(filterEH.test(200)).thenReturn(false); EventCollector<Listener, Integer> collector = createCollector(filter, filterEH); Listener target = collector.getTarget(); // when: target.event(100); target.event(200); // then: Mockito.verify(filter, Mockito.times(2)).test(Matchers.any()); Mockito.verify(filterEH, Mockito.times(2)).test(Matchers.any()); }
@Test public void ChainHandler_is_NOT_called() { // given: Predicate<Integer> filter = Mockito.mock(UberClasses.cast(Predicate.class)); EventHandler<Integer> handler = Mockito.mock(UberClasses.cast(EventHandler.class)); Mockito.when(filter.test(Matchers.any())).thenReturn(false); Mockito.when(handler.test(Matchers.any())).thenReturn(true); EventCollector<Listener, Integer> collector = EventCollector.create( Listener.class, Listener::event, EventHandlers.chain(filter, handler)); Listener target = collector.getTarget(); // when target.event(100); // then InOrder order = Mockito.inOrder(filter, handler); order.verify(filter, Mockito.times(1)).test(100); order.verifyNoMoreInteractions(); }
@Test public void events_are_collected() { // given: Predicate<Integer> filter = Mockito.mock(UberClasses.cast(Predicate.class)); EventHandler<Integer> filterEH = Mockito.mock(UberClasses.cast(EventHandler.class)); Mockito.when(filter.test(Matchers.any())).thenReturn(true); Mockito.when(filterEH.test(Matchers.any())).thenReturn(true); Mockito.when(filter.test(200)).thenReturn(false); EventCollector<Listener, Integer> collector = createCollector(filter, filterEH); Listener target = collector.getTarget(); // when: target.event(100); target.event(200); target.event(300); // then: Mockito.verify(filter, Mockito.times(3)).test(Matchers.any()); Mockito.verify(filterEH, Mockito.times(3)).test(Matchers.any()); // and: List<Integer> events = collector.getEvents(); Assert.assertThat(events, contains(100, 300)); Assert.assertThat(collector.getLastEvent(), equalTo(300)); }