如何使用匕首设计Android应用程序并考虑测试

Jam*_*rke 5 testing android unit-testing dependency-injection dagger

我是匕首的新手,最近我开始在自己的一个项目中使用匕首,因为能够以不同的方式处理依赖注入的概念用于测试和生产,因此能够注入我可以用于测试的模拟对象很棒.

我修改了我的应用程序,以遵循dagger simple-android示例中的样式.

设置完毕后,我发现注入存在问题,并且我无法使用测试逻辑完全超载生产应用程序中的注入.

我正在寻找关于如何设置它的建议,以便我的测试可以实际注入与模拟或其他对象的差异,以便根据需要进行测试,而不是过于愚蠢.目前,MainActivityTest被正确注入,但是当我们到达MainActivity时,它会转到PhoneApplication并使用它的对象图注入

我已经把我下面的内容包括在内了.任何帮助将不胜感激!


这是我的PhoneApplication,基于DemoApplication.

public class PhoneApplication extends Application {
    private ObjectGraph graph;

    @Override
    public void onCreate() {
        super.onCreate();

        graph = ObjectGraph.create(getModules().toArray());
    }

    protected List<Object> getModules() {
        return Arrays.asList(new AndroidModule(this), new PhoneModule());
    }

    public void inject(Object object) {
        graph.inject(object);
    }
}
Run Code Online (Sandbox Code Playgroud)

这是我的AndroidModule

@Module(library = true, injects = MainActivity.class)
public class AndroidModule {
    private final Context context;

    public AndroidModule(Context context) {
        this.context = context;
    }

    /**
     * Allow the application context to be injected but require that it be
     * annotated with {@link ForApplication @Annotation} to explicitly
     * differentiate it from an activity context.
     */
    @Provides
    @Singleton
    @ForApplication
    Context provideApplicationContext() {
        return context;
    }

    @Provides
    @Singleton
    NotificationManager provideNotificationManager() {
        return (NotificationManager) context
                .getSystemService(Application.NOTIFICATION_SERVICE);
    }

    @Provides
    @Singleton
    LocalBroadcastManager provideLocalBroadcastManager() {
        return LocalBroadcastManager.getInstance(context);
    }

    @Provides
    @Singleton
    ContentResolver provideContentResolver() {
        return context.getContentResolver();
    }

}
Run Code Online (Sandbox Code Playgroud)

基于该示例,我还将我的活动设置为使用基本活动.

public abstract class ActionBarBaseActivity extends ActionBarActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        ((PhoneApplication) getApplication()).inject(this);
    }
}
Run Code Online (Sandbox Code Playgroud)

然后在我的MainActivity中,我有以下内容

public class MainActivity extends ActionBarBaseActivity {

...

    @Inject
    LocalBroadcastManager localBroadcastManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
...
        try {
            messageReceivedIntentFilter = new IntentFilter(
                    Constants.EVENT_MESSAGE_RECEIVED,
                    "vnd.android.cursor.dir/vnd."
                            + DataProviderContract.AUTHORITY + "."
                            + DataProviderContract.MESSAGES_TABLE_NAME);

            localBroadcastManager.registerReceiver(messageReceiver,
                    messageReceivedIntentFilter);
        } catch (MalformedMimeTypeException e) {
            Log.e(LOG_TAG,
                    "An error occurred registering an Intent for EVENT_MESSAGE_RECEIVED",
                    e);
        }
...
    }
...
}
Run Code Online (Sandbox Code Playgroud)

这很有效,注射很快就到了,我欣喜若狂.直到我真的想做一些测试.我想要执行的第一个测试是在我的MainActivity上.

onCreate上面的方法中,我们使用AndroidModule中的LocalBroadcastManager 而不是MainActivityTest中的LocalBroadcastManager ,因为我们目前没有办法告诉PhoneApplication或活动他们应该使用不同的对象图.

public class MainActivityTest extends
        ActivityInstrumentationTestCase2<MainActivity> {

    @Inject
    NotificationManager notificationManager;

    @Inject
    ContentResolver contentResolver;

    @Inject
    MockContentResolver mockContentResolver;

    @Inject
    LocalBroadcastManager localBroadcastManager;

    private Context context;

    public MainActivityTest() {
        super(MainActivity.class);
    }

    @Module(injects = { MainActivityTest.class, MainActivity.class }, library = true, overrides = true)
    static class MockModule {
        Context context;

        public MockModule(Context context) {
            this.context = context;
        }

        @Provides
        @Singleton
        ContentResolver provideContentResolver() {
            return provideMockContentResolver();
        }

        @Provides
        @Singleton
        MockContentResolver provideMockContentResolver() {
            return new MockContentResolver();
        }

        @Provides
        @Singleton
        LocalBroadcastManager provideLocalBroadcastManager() {
            return Mockito.mock(LocalBroadcastManager.class);
        }
    }

    @Override
    protected void setUp() throws Exception {
        System.setProperty("dexmaker.dexcache", getInstrumentation()
                .getTargetContext().getCacheDir().getPath());

        context = getInstrumentation().getTargetContext();
        ObjectGraph graph = ObjectGraph.create(new AndroidModule(context),
                new MockModule(context));
        graph.inject(this);

        super.setUp();
    };

    @MediumTest
    @UiThreadTest
    public void testIncomingMessageReceiver_onReceive()
            throws MalformedMimeTypeException {

        ArgumentCaptor<BroadcastReceiver> receiverCaptor = ArgumentCaptor
                .forClass(BroadcastReceiver.class);
        Mockito.verify(localBroadcastManager, Mockito.atLeastOnce())
                .registerReceiver(receiverCaptor.capture(),
                        Mockito.any(IntentFilter.class));
    }
}
Run Code Online (Sandbox Code Playgroud)

这是一个让我入门的非常简单的测试.我知道在onCreate中,我们要注册一个BroadcastReceiver,所以让我们确保它已注册.由于测试具有mockLocalBroadcastManager,但活动使用生产LocalBroadcastManager,因此验证失败.

Ale*_*lko 0

我不确定。刚刚在网上搜索了如何正确使用 dagger 进行测试。

尽管如此,据我了解,它MainActivity从应用程序中获取其对象图。所以,这就是您必须插入MockModule.

为此,您应该创建 的子类PhoneApplication并重写getModules()方法以返回您的MockModule. 之后,您应该使用ActivityUnitTestCase.setApplication()(您的测试应该ActivityUnitTestCase首先子类化)来模拟应用程序。这应该可以解决问题。