Dagger是否支持ActivityInstrumentationTestCase2测试的依赖注入

Ste*_*ord 6 android dependency-injection functional-testing dagger

我试图在继承ActivityInstrumentationTestCase2的Android功能测试中使用Dagger.

设置代码如下所示:

@Override
protected void setUp() {
    // TODO Auto-generated method stub
    try {
        super.setUp();
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    ObjectGraph.create(new TestModule()).inject(this);
    this.activity = super.getActivity();
}`
Run Code Online (Sandbox Code Playgroud)

通过调用触发的OnCreate方法super.getActivity()不使用TestModule提供的类.但是如果我手动运行我的活动(在测试上下文之外),那么我的非测试模块会提供/注入所有相应的类.

Jas*_*son 3

ActivityInstrumentationTestCase2我找到了一种通过延迟创建对象图来使用 Dagger 的方法。我所做的是等到第一次要注入类时才创建对象图,因此只要在调用之前添加模块getActivity()(这会启动被测活动的活动生命周期)并overrides = true在测试模块中使用,这会起作用。这是相关的类和片段:

GraphHolder顾名思义,它ObjectGraph为我们保存了对象。我们将对此类进行所有调用,而不是直接对ObjectGraph.

public class GraphHolder {

    private static GraphHolder sInstance;

    private Object[] mModules;
    private ObjectGraph mGraph;

    private GraphHolder() {
    }

    public static GraphHolder getInstance() {
        if (sInstance == null) {
            sInstance = new GraphHolder();
        }

        return sInstance;
    }

    public void inject(Object object) {
        if (mGraph == null) {
            create();
        }

        mGraph.inject(object);
    }

    public <T> T get(Class<T> type) {
        if (mGraph == null) {
            create();
        }

        return mGraph.get(type);
    }

    public void addModules(Object... modules) {
        if (mGraph != null) {
            mGraph.plus(modules);
        } else {
            if (mModules == null) {
                mModules = modules;
            } else {
                mModules = concatenate(mModules, modules);
            }
        }
    }

    private void create() {
        mGraph = ObjectGraph.create(mModules);
        mModules = null;
    }

    private Object[] concatenate(Object[] a, Object[] b) {
        int aLength = a.length;
        int bLength = b.length;

        Object[] c = new Object[aLength + bLength];
        System.arraycopy(a, 0, c, 0, aLength);
        System.arraycopy(b, 0, c, aLength, bLength);

        return c;
    }
}
Run Code Online (Sandbox Code Playgroud)

我们将在Application类中添加我们的模块:

public class MyApplication extends Application {

    @Override
    public void onCreate() {
        super.onCreate();
        GraphHolder.getInstance().addModules(getModules());
    }

    Object[] getModules() {
        return new Object[]{
                // your modules here
        };
    }
}
Run Code Online (Sandbox Code Playgroud)

在我们想要注入的类中,我们只需调用GraphHolder.getInstance().inject(this)而不是ObjectGraph.inject(this)

在我们的测试模块中,我们将提供我们想要覆盖以进行测试的对象并添加overrides = true@Module注释中。这告诉对象图在发生冲突时优先选择该模块的提供者而不是其他提供者。

然后,在我们的测试中:

@Inject Foo mFoo;

@Override
public void setUp() {
    super.setUp();
    GraphHolder.getInstance().addModules(new TestFooModule());
    GraphHolder.getInstance().inject(this); // This is when the object graph will be created
}
Run Code Online (Sandbox Code Playgroud)