Guice测试是否已创建类

时间:2016-12-08 16:56:54

标签: java unit-testing dependency-injection guice

我想测试在测试方法期间是否从注入器创建了对象的实例。实现这一目标的最佳解决方案是什么?

@Test
public void testThingNotInstantiated() {
    AnotherThing another = new AnotherThing(); 
    // assert not instance of Thing created
}

1 个答案:

答案 0 :(得分:2)

如果您只想检查Guice注入OnClick,您可以写:

AnotherThing

如果Injector injector @Before { injector = Guice.createInjector(new AnotherThingModule()); } @Test public void testAnotherThingInstantiated() { //act AnotherThing another = injector.getInstance(AnotherThing.class); //assert assertNotNull(another); } AnotherThing并且你想测试Guice没有实例化它两次,你可以写:

@Singleton