001package io.dinject.core;
002
003import org.mockito.Mockito;
004
005import java.util.function.Consumer;
006
007/**
008 * Holds Spy setup consumers for dependency injection using Mockito Spy.
009 */
010public class EnrichBean<B> {
011
012  private final Class<B> type;
013
014  private final Consumer<B> consumer;
015
016  public EnrichBean(Class<B> type, Consumer<B> consumer) {
017    this.type = type;
018    this.consumer = consumer;
019  }
020
021  /**
022   * Return the dependency injection target type.
023   */
024  public Class<B> getType() {
025    return type;
026  }
027
028  /**
029   * Return the spy enhanced bean instance to use.
030   */
031  public B enrich(B bean) {
032
033    // should extract a SPI for this. Only enrichment is Mockito spy at this point.
034    B spy = Mockito.spy(bean);
035    if (consumer != null) {
036      consumer.accept(spy);
037    }
038    return spy;
039  }
040}