ReflectionObjectConstructor implements ObjectConstructor<T> { @Override public T applyDomainEvents(T instance, Collection<DomainEvent> events) { if (events == null || events.isEmpty()) { return instance; } events.forEach(event -> applyDomainEvent(instance, event)); invokePostConstructIfNeeded(instance); return instance; } ReflectionObjectConstructor(Class<T> targetClass); @Override T construct(Collection<DomainEvent> events); @Override T applyDomainEvents(T instance, Collection<DomainEvent> events);  }
@Test void whenGivenAnEmptyListOfEventsAndAnInstance_ShouldReturnInstance() { ObjectConstructor<MyAggregateRoot> objectConstructor = new ReflectionObjectConstructor<>(MyAggregateRoot.class); MyAggregateRoot instance = new MyAggregateRoot("id", "name"); MyAggregateRoot object = objectConstructor.applyDomainEvents(instance, Collections.emptyList()); assertThat(object, sameInstance(instance)); }
@Test void whenGivenAnNullListOfEventsAndAnInstance_ShouldReturnInstance() { ObjectConstructor<MyAggregateRoot> objectConstructor = new ReflectionObjectConstructor<>(MyAggregateRoot.class); MyAggregateRoot instance = new MyAggregateRoot("id", "name"); MyAggregateRoot object = objectConstructor.applyDomainEvents(instance, null); assertThat(object, sameInstance(instance)); }