【问题标题】:An elegant way to mock creation of AsyncTasks and Intents in Android code在 Android 代码中模拟创建 AsyncTasks 和 Intents 的优雅方式
【发布时间】:2013-06-27 16:31:24
【问题描述】:

我有一个 Android 活动,其中有类似的调用

final ConnectToServerAsyncTask task = new ConnectToServerAsyncTask(...);

和

final Intent intent = new Intent(this, SomeActivity.class);

为了对这个类进行单元测试,我需要能够模拟 ConnectToServerAsyncTask 和 Intent 的创建(例如使用 Mockito)。

还有比下面描述的更优雅的方法吗?

public class MainActivityOfTheApp extends Activity {
    private IAsyncTaskFactory asyncTaskFactory = new AsyncTaskFactory();
    private IIntentFactory intentFactory = new IntentFactory();

    public void setAsyncTaskFactory(final IAsyncTaskFactory aFactory)
    {
        asyncTaskFactory = aFactory;
    }

    public void setIntentFactory(final IIntentFactory aFactory)
    {
        intentFactory = aFactory;
    }

    @Override
    protected void onResume() {
        ...
        final ConnectToServerAsyncTask task = asyncTaskFactory.create(...);
        ...

        final Intent intent = intentFactory.create(this, OtherActivity.class);
        ...
    }
}

在单元测试中,我会创建一个 MainActivityOfTheApp 实例,然后使用 setAsyncTaskFactory 和 setIntentFactory 注入模拟。

【问题讨论】:

  • 你的解决方案也是我能想到的最聪明的。我也很想知道是否有其他方法。

标签: java android unit-testing mocking mockito


【解决方案1】:

PowerMock

您可以使用PowerMock 模拟构造函数。

所以在你的情况下,测试看起来像这样:

@RunWith(PowerMockRunner.class)
@PrepareForTest(MainActivityOfTheApp.class)
public class MainActivityOfTheAppTest{
  private AsyncTaskFactory asyncTaskFactory;
  private IntentFactory intentFactory;
  private MainActivityOfTheApp mainActivityOfTheApp;

  @Before
  public void prepare() {
    asyncTaskFactory = PowerMockito.mock(AsyncTaskFactory.class);
    intentFactory = PowerMockito.mock(IntentFactory.class);
    PowerMockito.whenNew(AsyncTaskFactory.class).withNoArguments().thenReturn(asyncTaskFactory);
    PowerMockito.whenNew(IntentFactory.class).withNoArguments().thenReturn(intentFactory);
    mainActivityOfTheApp = new MainActivityOfTheApp();
  }

  @Test
  public void doTest() {
    //mainActivityOfTheApp has the mocks in final field inside, no need for the setters.
  }
}

我必须指出,PowerMock 功能强大,但经常遇到复杂的类加载(例如 OSGI 环境)或代码覆盖工具(例如 jacoco)的问题。它通常有效,但我浪费了一些时间。

没有 PowerMock

第二种可能性是创建package private(没有修饰符。为什么不保护或公开?参见What's wrong with overridable method calls in constructors?) 方法,它像这样调用构造函数:

public class MainActivityOfTheApp extends Activity {
    private final IAsyncTaskFactory asyncTaskFactory = constructAsyncTaskFactory();
    private final IIntentFactory intentFactory = constructIntentFactory();

    IAsyncTaskFactory constructAsyncTaskFactory()
    {
        return new AsyncTaskFactory();
    }

    IIntentFactory constructIntentFactory()
    {
        return new IntentFactory();
    }
    ...
}

然后在你的单元测试中(测试必须和被测试的类在同一个包中!)你重写construct*()方法:

public class MainActivityOfTheAppTest{
  private AsyncTaskFactory asyncTaskFactory;
  private IntentFactory intentFactory;
  private MainActivityOfTheApp mainActivityOfTheApp;

  @Before
  public void prepare() {
    asyncTaskFactory = mock(AsyncTaskFactory.class);
    intentFactory = mock(IntentFactory.class);
    mainActivityOfTheApp = new HackedMainActivityOfTheApp();
  }

  @Test
  public void doTest() {
    //mainActivityOfTheApp has the mocks in final field inside, no need for the setters.
  }

  private class HackedMainActivityOfTheApp extends MainActivityOfTheApp {
        IAsyncTaskFactory constructAsyncTaskFactory()
        {
            return asyncTaskFactory;
        }

        IIntentFactory constructIntentFactory()
        {
            return intentFactory;
        }
  }
}

如果您对包含 MainActivityOfTheApp 的 jar 进行签名,则可以保护construct* 方法。见https://stackoverflow.com/a/968612/337621:

此外,补丁更难(您必须重新签名 jar),类补丁是不可能的(单个包中的所有类必须具有相同的签名源)并且拆分 jar 变得很麻烦。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-12
    • 1970-01-01
    • 1970-01-01
    • 2011-06-07
    相关资源
    最近更新 更多