【问题标题】:How to get Guice injector for a class with parameterised constructor如何为具有参数化构造函数的类获取 Guice 注入器
【发布时间】:2020-11-19 23:18:08
【问题描述】:

我现在编写了以下课程和测试代码,它工作正常。

public class AppleRegistry {
  private final ImmutableMap<String, Apple> appRegistry;
  
  AppleRegistry(Apple... apple) {
    this.appRegistry = createRegistry(apple);
  }

  @Inject
  AppleRegistry(
    Provider<GoodApple> goodAppleProvider,
    Provider<BadApple> badAppleProvider,
    Provider<BrandedApple> brandedAppleProvider) {
      this(goodAppleProvider.get(), badAppleProvider.get(), brandedAppleProvider.get());
    }
  
  private static ImmutableMap<String, Apple> createRegistry(Apple... apple){......}
  
  public Apple findApple(String key) {....}
}

public interface Apple{
...
}

public class GoodApple implements Apple {
  private final Producer producer;
  private final Suuplier supplier;

  @Inject
  GoodApple(Producer producer, Supplier supplier) {....}
....
}

public class BadApple implements Apple {.....}
public class BrandedApple implements Apple {....}

Test class
@RunWith(JUnit4.class)
@SmallTest
public class AppleRegistryTest {

  @Before
  public void setUp() throws Exception {
    Guice.createInjector().injectMemeber(this);
  }

  @Test
  public void testFindApple_emptyRegistry() {
    Optional<Apple> apple = new AppleRegistry().findApple("First");
    assertFalse(apple.isPresent());
  }
}

到这里一切正常并且测试通过了。 现在我想在 AppleRegistryClass 中引入新的依赖 以下是变化

public class AppleRegistry {
 private final ImmutableMap<String, Apple> appRegistry;
 private final SystemHelper systemHelper;
      
 AppleRegistry(SystemHelper systemHelper, Apple... apple) {
    this.systemHelper = systemHelper;
    this.appRegistry = createRegistry(apple);
  }

  @Inject
  AppleRegistry(
    Provider<GoodApple> goodAppleProvider,
    Provider<BadApple> badAppleProvider,
    Provider<BrandedApple> brandedAppleProvider
    SystemHelper systemHelper) {
      this(systemHelper, goodAppleProvider.get(), badAppleProvider.get(), brandedAppleProvider.get());
    }
  
  private static ImmutableMap<String, Apple> createRegistry(Apple... apple){......}
  
  public Apple findApple(String key) {....}
}

// This is an existing class
@Singleton
public class SystemHelper {
  private final SystemDao systemDao;

  @inject
  public SystemHelper(SystemDao systemDao) {
    this.systemDao = systemDao;
  }
....
}

public class SystemDao {
  private final SpannerProtoDao<SystemConfig> spannerProtoDao;

  @Inject
  public SystemDao(@AbcDatabase Database abcDatabase) {
    spannerProtoDao = SpannerProtoDao.newBuilder(SystemConfig.class)
                         .setDatabase(abcDatabase)
                         .setTable("Table").setMessageColumn("column")
                         .build();
  }
}

现在,当我为新更改编写测试时,它会超时 测试代码:

@RunWith(JUnit4.class)
@SmallTest
public class AppleRegistryTest {

  private final SystemHelper systemHelper;

  @Before
  public void setUp() throws Exception {
    Guice.createInjector((Module) new SystemHelper(new SystemDao(TestUtil.createDatabase()))).injectMemeber(this);
  }

  @Test
  public void testFindApple_emptyRegistry() {
    Optional<Apple> apple = new AppleRegistry(systemHelper).findApple("First");
    assertFalse(apple.isPresent());
  }
}

有人可以帮我编写 AppleRegistry 的测试类吗? 提前致谢!

【问题讨论】:

    标签: java guice guice-3


    【解决方案1】:

    我做了这样的事情。 这是一种糟糕的编码习惯吗?

    public class AppleRegistryTest {
      @Rule public final Mocks mocks = new Mocks(this);
      @Mock private SystemHelper systemHelper;
    
      @Test
      public void testFindApple_emptyRegistry() {
        Mockito.when(systemHelper.isXyzEnabled()).thenReturn(False);
        Apple apple = new AppleRegistry(systemHelper).findApple("First");
        assertFalse(apple.isPresent()); 
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-07-14
      • 2018-02-25
      • 2017-09-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-26
      相关资源
      最近更新 更多