【问题标题】:How to doNothing() on void method?如何在 void 方法上做 doNothing()?
【发布时间】:2015-10-12 15:17:19
【问题描述】:

我有一个调用 void 函数的方法,当我使用 doNothing() 时,它说不允许使用 void 方法。我怎么能在那个特定的行中doNothing()

我正在使用这条线,

when(spyColorSelector.initializeColors(view, "red")).then(doNothing());

【问题讨论】:

  • 这是完整的行,还是你想返回一些东西?
  • 我只想要那个 initializeColors(),不会被执行
  • 您希望什么时候发生这种情况?为什么不使用 if 块?

标签: android unit-testing testing mockito


【解决方案1】:

使用Stubber 语法:

doNothing().when(spyColorSelector).initializeColors(view, "red");

spyColorSelector 必须是一个模拟。


编辑 1:带有间谍的代码示例。

此测试适用于 JUnit 4.12 和 Mockito 1.10.19(initializeColors 不会引发异常):

public class ColorSelectorTest {

    @Test
    public void testGetColors() {
        // Given
        String color = "red";
        View view = mock(View.class);
        ColorSelector colorSelector = new ColorSelector();
        ColorSelector spyColorSelector = spy(colorSelector);
        doNothing().when(spyColorSelector).initializeColors(view, color);

        // When
        LinkedList<Integer> colors = spyColorSelector.getColors(color, view);

        // Then
        assertNotNull(colors);
    }
}

class ColorSelector {

    public LinkedList<Integer> getColors(String color, View view) {
        this.initializeColors(view, color);
        return new LinkedList<>();
    }

    void initializeColors(View view, String color) {
        throw new UnsupportedOperationException("Should not be called");
    }
}

编辑 2:没有间谍的新示例。

如果你真的不希望initializeColors 在测试中被执行,我认为ColorSelector 类中存在设计问题。 initializeColors 方法应该在另一个类 X 中,并且在 ColorSelector 类中会有类 X 的依赖关系,您可以在测试中存根(然后不需要间谍)。基本示例:

public class ColorSelectorTest {

    @Test
    public void testGetColors() {
        // Given
        String color = "red";
        View view = mock(View.class);
        ColorSelector colorSelector = new ColorSelector();
        ColorInitializer colorInitializerMock = mock(ColorInitializer.class);
        doNothing().when(colorInitializerMock).initializeColors(view, color);   // Optional because the default behavior of a mock is to do nothing
        colorSelector.colorInitializer = colorInitializerMock;

        // When
        LinkedList<Integer> colors = colorSelector.getColors(color, view);

        // Then
        assertNotNull(colors);
    }
}

class ColorSelector {

    ColorInitializer colorInitializer;

    public LinkedList<Integer> getColors(String color, View view) {
        colorInitializer.initializeColors(view, color);
        return new LinkedList<>();
    }
}

class ColorInitializer {

    public void initializeColors(View view, String color) {
        // Do something
    }
}

【讨论】:

  • 我试过了,还是一样。当我调试时,测试进入该方法。
  • 你如何初始化你的模拟?你能复制粘贴你的整个测试吗?
  • @Test public void testGetColors(){ View view = mock(View.class); doNothing().when(spyColorSelector).initializeColors(eq(view), eq("red")); LinkedList&lt;Integer&gt; colors = spyColorSelector.getColors("red", view); assertNotNull(colors); } initializeColors() 在 getColors() 方法中
  • 好的。我会尽快回复。你的 spyColorSelector 是如何在你的测试类中初始化的?
  • ColorSelector spyColorSelector = spy(colorSelector);
猜你喜欢
  • 1970-01-01
  • 2014-09-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多