@SuppressWarnings("resource") @Test public void updateEntityWithListeners() { @SuppressWarnings("unchecked") Repository<Entity> decoratedRepository = Mockito.mock(Repository.class); Mockito.when(decoratedRepository.getName()).thenReturn("entityFullName"); EntityListenerRepositoryDecorator entityListenerRepositoryDecorator = new EntityListenerRepositoryDecorator(decoratedRepository, entityListenersService); EntityListener entityListener0 = Mockito.when(Mockito.mock(EntityListener.class).getEntityId()) .thenReturn(Integer.valueOf(1)) .getMock(); EntityListener entityListener1 = Mockito.when(Mockito.mock(EntityListener.class).getEntityId()) .thenReturn(Integer.valueOf(1)) .getMock(); entityListenersService.addEntityListener("entityFullName", entityListener0); entityListenersService.addEntityListener("entityFullName", entityListener1); Entity entity = Mockito.when(Mockito.mock(Entity.class).getIdValue()) .thenReturn(Integer.valueOf(1)) .getMock(); entityListenerRepositoryDecorator.update(entity); Mockito.verify(decoratedRepository).update(entity); Mockito.verify(entityListener0, Mockito.times(1)).postUpdate(entity); Mockito.verify(entityListener1, Mockito.times(1)).postUpdate(entity); }
@SuppressWarnings({"resource", "unchecked", "rawtypes"}) @Test public void updateStreamWithSomeListeners() { Repository<Entity> decoratedRepository = Mockito.mock(Repository.class); Mockito.when(decoratedRepository.getName()).thenReturn("entityFullName"); EntityListenerRepositoryDecorator entityListenerRepositoryDecorator = new EntityListenerRepositoryDecorator(decoratedRepository, entityListenersService); EntityListener entityListener1 = Mockito.when(Mockito.mock(EntityListener.class).getEntityId()) .thenReturn(Integer.valueOf(2)) .getMock(); entityListenersService.addEntityListener("entityFullName", entityListener1); Entity entity0 = Mockito.when(Mockito.mock(Entity.class).getIdValue()) .thenReturn(Integer.valueOf(1)) .getMock(); Entity entity1 = Mockito.when(Mockito.mock(Entity.class).getIdValue()) .thenReturn(Integer.valueOf(2)) .getMock(); Stream<Entity> entities = Stream.of(entity0, entity1); entityListenerRepositoryDecorator.update(entities); ArgumentCaptor<Stream<Entity>> captor = ArgumentCaptor.forClass((Class) Stream.class); Mockito.verify(decoratedRepository).update(captor.capture()); Assert.assertEquals( captor.getValue().collect(Collectors.toList()), Arrays.asList(entity0, entity1)); Mockito.verify(entityListener1, Mockito.times(1)).postUpdate(entity1); }