Java 正在尝试将junit框架从dagger1迁移到dagger2
我一直在试图找到一种合适的方法,将我们当前的测试自动化库从使用dagger1迁移到dagger2,但我正在与一般概念作斗争,因为dagger2不允许协方差 我已经通读了我能找到的每一篇文章,但我对依赖注入还是相当陌生的。我读过的例子很有道理,但它们似乎没有涵盖我现在面临的情况 我一直在试图让我的头脑围绕着组件如何适合这种特殊设计的一般概念,但我担心我可能忽略了一些显而易见的东西。如果有必要的话,我愿意接受全面检查,但我更希望这是最后的手段 如能提供任何有用资源的示例或链接,将不胜感激Java 正在尝试将junit框架从dagger1迁移到dagger2,java,junit,dagger-2,Java,Junit,Dagger 2,我一直在试图找到一种合适的方法,将我们当前的测试自动化库从使用dagger1迁移到dagger2,但我正在与一般概念作斗争,因为dagger2不允许协方差 我已经通读了我能找到的每一篇文章,但我对依赖注入还是相当陌生的。我读过的例子很有道理,但它们似乎没有涵盖我现在面临的情况 我一直在试图让我的头脑围绕着组件如何适合这种特殊设计的一般概念,但我担心我可能忽略了一些显而易见的东西。如果有必要的话,我愿意接受全面检查,但我更希望这是最后的手段 如能提供任何有用资源的示例或链接,将不胜感激 以下是原
以下是原始(dagger1)设计的代码片段 自动JUnitRunner图形实例化
@Override
protected Object createTest() throws Exception
{
Object test = super.createTest();
if(test instanceof RequiresInjection)
{
WebDriverModule module = new WebDriverModule(capabilities, isProxiedWebDriver);
LOGGER.trace("create test called " + test.getClass().getSimpleName() + " - " + module.provideBrowserName());
ObjectGraph objectGraph = ObjectGraph.create(concat(getModules((RequiresInjection) test), module));
objectGraph.inject(test);
}
return test;
}
private Object[] getModules(RequiresInjection requiresInjection)
{
Object modules = requiresInjection.getModules();
return modules instanceof Object[] ? (Object[]) modules : new Object[] { modules };
}
private Object[] concat(Object[] modules, Object module)
{
Object[] result = Arrays.copyOf(modules, modules.length + 1);
result[modules.length] = module;
return result;
}
@Override
protected Object createTest() throws Exception
{
Object test = super.createTest();
if (test instanceof AutomatedTest)
{
WebDriverModule module = new WebDriverModule(capabilities, isProxiedWebDriver);
LOGGER.trace("create test called " + test.getClass().getSimpleName() + " - " + module.provideBrowserName());
ConfigurationComponent cc = DaggerConfigurationComponent.builder().build();
WebDriverComponent wdc = DaggerWebDriverComponent.builder().webDriverModule(module).build();
AutomationComponent automationComponent = DaggerAutomationComponent.builder()
.configurationComponent(cc)
.webDriverComponent(wdc)
.build();
((AutomatedTest) test).inject(automationComponent);
}
return test;
}
样本匕首1原始测试类别和测试模块
@RunWith(AutomationJUnitRunner.class)
public class LoginTest implements RequiresInjection
{
@Inject
@Rule
public WebDriverRule webDriverRule;
@Inject
LoginNavigator loginNavigator;
@Inject
Credentials credentials;
@Test
public void login()
{
LoginPage loginPage = loginNavigator.getLoginPage();
DashboardPage dashboard = loginPage.login(credentials.getUsername(), credentials.getPassword());
assertThat(dashboard.isVisible(), is(true));
}
@Override
public Object getModules()
{
return new TestModule();
}
@Module(injects = LoginTest.class, includes = { WebDriverModule.class }, overrides = true)
public class TestModule
{
Credentials provideCredentials()
{
return new Credentials("exampleuser", "examplepass");
}
}
Dagger2尝试-测试类和测试组件
@RunWith(AutomationJUnit4Runner.class)
public class LoginTest implements AutomatedTest
{
@Inject
@Rule
public WebDriverRule webDriverRule;
@Inject
LoginNavigator loginNavigator;
@Inject
Credentials credentials;
@Test
public void login()
{
LoginPage loginPage = loginNavigator.getLoginPage();
DashboardPage dashboard = loginPage.login(credentials.getUsername(), credentials.getPassword());
assertThat(dashboard.isVisible(), is(true));
}
@Override
public void inject(AutomationComponent automationComponent)
{
LoginTestComponent c = DaggerLoginTest_LoginTestComponent
.builder()
.automationComponent(automationComponent)
.build();
c.inject(this);
}
@Component(dependencies = { AutomationComponent.class }, modules = { CredentialsModule.class })
public interface LoginTestComponent
{
void inject(LoginTest loginTest);
}
}
Dagger2尝试-测试运行程序,测试实例化
@Override
protected Object createTest() throws Exception
{
Object test = super.createTest();
if(test instanceof RequiresInjection)
{
WebDriverModule module = new WebDriverModule(capabilities, isProxiedWebDriver);
LOGGER.trace("create test called " + test.getClass().getSimpleName() + " - " + module.provideBrowserName());
ObjectGraph objectGraph = ObjectGraph.create(concat(getModules((RequiresInjection) test), module));
objectGraph.inject(test);
}
return test;
}
private Object[] getModules(RequiresInjection requiresInjection)
{
Object modules = requiresInjection.getModules();
return modules instanceof Object[] ? (Object[]) modules : new Object[] { modules };
}
private Object[] concat(Object[] modules, Object module)
{
Object[] result = Arrays.copyOf(modules, modules.length + 1);
result[modules.length] = module;
return result;
}
@Override
protected Object createTest() throws Exception
{
Object test = super.createTest();
if (test instanceof AutomatedTest)
{
WebDriverModule module = new WebDriverModule(capabilities, isProxiedWebDriver);
LOGGER.trace("create test called " + test.getClass().getSimpleName() + " - " + module.provideBrowserName());
ConfigurationComponent cc = DaggerConfigurationComponent.builder().build();
WebDriverComponent wdc = DaggerWebDriverComponent.builder().webDriverModule(module).build();
AutomationComponent automationComponent = DaggerAutomationComponent.builder()
.configurationComponent(cc)
.webDriverComponent(wdc)
.build();
((AutomatedTest) test).inject(automationComponent);
}
return test;
}
您需要指定具体的类才能使用
void inject(
EpicPandaForce就在这里。如果您想向LoginTest类中注入变量,您需要使用void inject(LoginTest);Ty!我现在已经有了一个工作原型。我将更新示例,因为我希望尽可能地消除样板文件。