如何在没有第三方框架测试一个Android活动时注入的依赖? [英] How to inject a dependency when testing an Android activity without a third-party framework?

查看:142
本文介绍了如何在没有第三方框架测试一个Android活动时注入的依赖?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想测试一个Android活动 CommentActivity 通常构造和使用 CommentsDataSource 的一个实例(无论是类我写的)。

I want to test an Android activity CommentActivity that normally constructs and uses an instance of CommentsDataSource (both are classes that I wrote).

public class CommentActivity extends Activity {
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    :
    CommentsDataSource = new CommentsDataSource(..);
    :
  }
  :
}

我愿意创建 MockCommentsDataSource 我和想避免使用第三方嘲讽框架。 (为什么?因为我试图减少的信息,我需要塞进学期的数量和软件我的学生需要安装量的教导。我见过的其他职位的建议吉斯,roboguice和Spring)。

I'm willing to create MockCommentsDataSource myself and would like to avoid using a third-party mocking framework. (Why? Because I'm a teaching trying to reduce the amount of information I need to cram into the semester and the amount of software my students need to install. I've seen other posts that recommend Guice, roboguice, and Spring.)

我的问题是如何通过一个 CommentsDataSource (或 MockCommentsDataSource )的活动。这似乎并不现实,使他们序列化 Parcelable ,他们将不得不以传递在通过意图启动 CommentActivity 。虽然我可以很容易地通过一个调试标志,使用它需要 CommentActivity 了解 MockCommentsDataSource ,这真是没话说其业务(和在一个单独的应用程序):

My question is how to pass a CommentsDataSource (or MockCommentsDataSource) to the Activity. It doesn't seem practical to make them Serializable or Parcelable, which they would have to be in order to be passed in through the Intent that starts CommentActivity. While I could easily pass in a debug flag, using it would require CommentActivity to know about MockCommentsDataSource, which is really none of its business (and in a separate application):

public class CommentActivity extends Activity {
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    :
    debugMode = getIntent().getBooleanExtra(DEBUG_MODE, false);

    // Get a connection to the database.
    final CommentsDataSource cds = (debugMode ? 
      new MockCommentsDataSource() :   // Abstraction violation
      new CommentsDataSource(this));
      :
   }
   :
}

我应该如何注入 MockCommentsDataSource CommentActivity ? FWIW,我使用Eclipse和正在开发的最新SDK版本。

How should I inject MockCommentsDataSource into CommentActivity? FWIW, I'm using Eclipse and am developing for recent SDK versions.

这发生在我的一个解决方案是使用抽象工厂模式,因为这将是比较容易使工厂序列化。是最好的办法,给我的限制?

One solution that occurs to me is to use the abstract factory pattern, since it would be relatively easy to make the factories serializable. Is that the best approach, given my constraints?

推荐答案

下面是两个思路:

不使用厂家:

这可能会为单元测试只是工作,而不是集成测试:

This will probably work only for unit tests and not for integration tests:


  1. 创建一个返回CommentsDataSource,例如方法getCommentsDataSource()

  2. 创建一个继承CommentActivity类

  3. 与返回MockCommentsDataSource方法重载getCommentsDataSource()

  4. 测试新的类

使用工厂:

正如你提到的,你可以改变CommentActivity code从一个工厂方法获得CommentsDataSource。这样,你可以通过工厂方法返回的模拟类。

As you mentioned, you can change the CommentActivity code to get the CommentsDataSource from a factory method. this way you can have the mock class returned by the factory method.

希望这有助于!

这篇关于如何在没有第三方框架测试一个Android活动时注入的依赖?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆