How to doNothing() on void method?

49,884

Use Stubber syntax :

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

And spyColorSelector has to be a mock.


Edit 1: code example with spy.

This test works (no exception thrown by initializeColors) with JUnit 4.12 and Mockito 1.10.19:

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");
    }
}

Edit 2: new example without spy.

If you really want initializeColors not to be executed in the test, I think there is a design issue in the ColorSelector class. The initializeColors method should be in another class X, and there would be a dependency of class X in ColorSelector class which you could stub in your test (and then no need of spy). Basic example:

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
    }
}
Share:
49,884
Jimmy A. León
Author by

Jimmy A. León

Software Developer Mobico TIC S.A.S - Colombia Known languages: Java Android C++ SQL

Updated on July 23, 2020

Comments

  • Jimmy A. León
    Jimmy A. León almost 4 years

    I have a method that call a void function in it, and when I use doNothing(), it says that void method it's not allowed. How could I doNothing() in that specific line?

    I'm using this line,

    when(spyColorSelector.initializeColors(view, "red")).then(doNothing());
    
  • Jimmy A. León
    Jimmy A. León almost 9 years
    I tried that way and still the same. When I debug, the test get into that method.
  • Damien Beaufils
    Damien Beaufils almost 9 years
    How do you initialize your mock? Could you copy paste your whole test?
  • Jimmy A. León
    Jimmy A. León almost 9 years
    @Test public void testGetColors(){ View view = mock(View.class); doNothing().when(spyColorSelector).initializeColors(eq(view)‌​, eq("red")); LinkedList<Integer> colors = spyColorSelector.getColors("red", view); assertNotNull(colors); } initializeColors() is inside getColors() method
  • Damien Beaufils
    Damien Beaufils almost 9 years
    Ok. I'll answer asap. And how is your spyColorSelector initialized in your test class?
  • Jimmy A. León
    Jimmy A. León almost 9 years
    ColorSelector spyColorSelector = spy(colorSelector);
  • Jose Martinez
    Jose Martinez almost 9 years
    @JianLeón, can you post the error (and stack trace if there is one) you get?
  • Jimmy A. León
    Jimmy A. León almost 9 years
    I got this in "Edit 1": E.g. thenReturn() may be missing. Examples of correct stubbing: when(mock.isOk()).thenReturn(true); when(mock.isOk()).thenThrow(exception); doThrow(exception).when(mock).someVoidMethod(); Hints: 1. missing thenReturn() 2. you are trying to stub a final method, you naughty developer!
  • Jimmy A. León
    Jimmy A. León almost 9 years
    initializeColors() method, initialize local variables, and It's better for me to leave that way. It makes me easier the way I'm gonna work that funtionality