Mockito is a powerful framework, which allows us to mock classes, method behavior etc. Sometimes we would like to mock method call with null value as the parameter.

Let’s suppose we have a Foo class which autowires Bar class. We have following test class for Foo:

@RunWith(MockitoJUnitRunner.class)
public class FooTest {

    @Mock
    private Bar bar;

    @InjectMocks
    private Foo foo;

    @Test
    public shouldReturnSomehing() {
        when(bar.process(eq(null))).thenReturn("none");

        assertEquals("none", foo.build("sth", null));
    }
}

This construction works perfectly previously, but will not work with Java 8 and Mockito 2.3 or newer.

For the Java 8 and Mockito 2.3 it should be called this way:

when(bar.process(isNull())).thenReturn("none");
Mock null value as the parameter of called method (Java 8)
Tagged on:                         

Leave a Reply

Your email address will not be published. Required fields are marked *

Social Widgets powered by AB-WebLog.com.