[英]Guice test whether class has been created
我想測試在測試方法期間是否從注入器創建了對象的實例。 實現此目標的最佳解決方案是什么。
@Test
public void testThingNotInstantiated() {
AnotherThing another = new AnotherThing();
// assert not instance of Thing created
}
如果您只想檢查Guice是否注入了AnotherThing
,則可以編寫:
Injector injector
@Before {
injector = Guice.createInjector(new AnotherThingModule());
}
@Test
public void testAnotherThingInstantiated() {
//act
AnotherThing another = injector.getInstance(AnotherThing.class);
//assert
assertNotNull(another);
}
如果AnotherThing
是@Singleton
,你想測試吉斯不會實例兩次,你可以這樣寫:
@Test
public void testSingletonAnotherThingNotInstantiatedTwiceByInjector() {
//act
AnotherThing first = injector.getInstance(AnotherThing.class);
AnotherThing second = injector.getInstance(AnotherThing.class);
//assert
assertSame(first, second);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.