【问题标题】:Casting a parameter mock object to another object using Mockito使用 Mockito 将参数模拟对象转换为另一个对象
【发布时间】:2013-08-09 15:17:40
【问题描述】:

我有一个测试方法,开始如下:

public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
    AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;
    contextString = adapter.getItem(info.position);
        /.../
    }

我想使用 Mockito 对其进行测试,但如果我这样声明 menuInfo:

@Mock ContextMenuInfo menuInfo

那么我无法编译以下语句:

Mockito.when(menuInfo.position).thenReturn(1);

因为它对ContextMenuInfo 对象无效。我无法将我的对象声明为 AdapterView.AdapterContextMenuInfo 类,因为那时我在运行时遇到错误。

我知道在 Mockito 中,一个 mock 可能实现多个接口,但同样不适用于类。如何测试我上面显示的方法?

【问题讨论】:

  • 我收到一个错误:哪个错误?
  • org.mockito.exceptions.misusing.MissingMethodInvocationException: when() 需要一个必须是“模拟方法调用”的参数。

标签: java android junit mockito


【解决方案1】:

Mockito 通过使用 Java 继承 来替换类上方法的实现。但是,看起来position 是AdapterContextMenuInfo 上的一个字段,这意味着Mockito 无法为您模拟它。

幸运的是,AdapterContextMenuInfo 有a public constructor,所以你不必 模拟它——您可以为测试创建一个并将其传递给您的方法。

@Test public void contextMenuShouldWork() {
  ContextMenuInfo info =
      new AdapterView.AdapterContextMenuInfo(view, position, id);
  systemUnderTest.onCreateContextMenu(menu, view, info);

  /* assert success here */
}

如果您曾经因无法直接模拟或实例化的类而陷入这种模式,请考虑创建一个您可以模拟的帮助器类:

class MyHelper {

  /** Used for testing. */
  int getPositionFromContextMenuInfo(ContextMenuInfo info) {
    return ((AdapterContextMenuInfo) info).position;
  }
}

现在您可以重构您的视图以使用它:

public class MyActivity extends Activity {
  /** visible for testing */
  MyHelper helper = new MyHelper();

  public void onCreateContextMenu(
      ContextMenu menu, View v, ContextMenuInfo menuInfo) {
    int position = helper.getPositionFromContextMenuInfo(menuInfo);
    // ...
  }
}

...然后在您的测试中模拟 helper。

/** This is only a good idea in a world where you can't instantiate the type. */
@Test public void contextMenuShouldWork() {
  ContextMenuInfo info = getSomeInfoObjectYouCantChange();

  MyHelper mockHelper = Mockito.mock(MyHelper.class);
  when(mockHelper.getPositionFromContextMenu(info)).thenReturn(42);
  systemUnderTest.helper = mockHelper;
  systemUnderTest.onCreateContextMenu(menu, view, info);

  /* assert success here */
}    

还有另一种选择,涉及重构:

public class MyActivity extends Activity {
  public void onCreateContextMenu(
      ContextMenu menu, View v, ContextMenuInfo menuInfo) {
    AdapterView.AdapterContextMenuInfo info =
        (AdapterView.AdapterContextMenuInfo) menuInfo;
    onCreateContextMenuImpl(info.position);
  }

  /** visible for testing */
  void onCreateContextMenuImpl(int position) {
    // the bulk of the code goes here
  }
}

@Test public void contextMenuShouldWork() {
  systemUnderTest.onCreateContextMenuImpl(position);

  /* assert success here */
}

【讨论】:

  • 感谢您的完整回答。在这种情况下,我实施了第一个选项。但是,第二个选项非常有用,我需要它用于另一种情况。在那里我仍然有一个为模拟设置字段值的问题。即:我模拟了一个帮助器类,并且可以将它作为可测试类的参数传递,但是我如何以 when(myHelper.position).thenReturn((float)100) 的模拟方式设置字段值?
  • 更新了我的答案,详细说明了如何模拟助手。希望这能让事情更清楚! :)
  • 非常感谢,建议的解决方案都描述得很好:)
【解决方案2】:

可以使用 mockito 的 extraInterfaces 选项

        @Mock(extraInterfaces=AdapterView.AdapterContextMenuInfo.class) 
        ContextMenuInfo menuInfo

然后像这样模拟它

Mockito.doReturn(1).when((AdapterView.AdapterContextMenuInfo)menuInfo).position

【讨论】:

    【解决方案3】:

    为什么在AdapterView.AdapterContextMenuInfo 中没有一个getter 可以让你写info.getPosition()?如果你有这个,那么你可以模拟AdapterView.AdapterContextMenuInfo,存根getPosition(),然后只需将模拟传递给你正在测试的方法。

    【讨论】:

    • 感谢您的建议。 :) 但是,AdapterView.AdapterContextMenuInfo 是一个 Android 框架类,所以我想重构该代码将是一个不寻常的解决方案。
    • 哦,抱歉,我没有意识到它是 Android 的一部分 :-) 我不是 Android 开发人员。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-08-14
    • 2017-10-15
    • 2019-10-20
    • 2018-05-08
    • 2021-12-13
    • 2015-09-03
    相关资源
    最近更新 更多