我正在开发一个使用TestNG的测试自动化框架。我决定使用依赖注入模式来实现更具可读性、可重用的页面对象和测试。
我之所以选择Google Guice,是因为TestNG提供了内置支持来使用Guice Modules注入测试对象。我只需要指定我的Guice 模块,您可以在下一个代码片段中看到:
@Guice(modules = CommModule.class)
public class CommunicationTest {
@Inject
private Communication comms;
@Test
public void testSendMessage() {
Assertions.assertThat(comms.sendMessage("Hello World!")).isTrue();
}
}
到目前为止一切顺利,尽管我需要更多高级 DI 功能,例如:
- 生命周期管理
- 配置到字段映射
- 通用绑定注释
因此,我想使用Netflix/Governator,因为它通过这些功能增强了Google Guice 。为了触发Governator功能,我必须Injector
通过它而不是TestNG创建。例如:
Injector injector = LifecycleInjector.builder()
.withModules(CommModules.class).build().createInjector();
而且我想尽可能透明地做到这一点,就像TestNG所做的那样。
我想知道是否:
- 是否可以向TestNG提供我自己的
Injector
实例以重用注释方法?@Guice
- 您知道任何将Governator与TestNG集成的库吗?
你可以在这里找到我到目前为止所做的事情。