Mockito: doReturn() vs when()

import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.spy; import static org.mockito.Mockito.when; import org.junit.Assert; import org.junit.Test; public class MockitoTest { @Test public void test() { X x = spy(X.class); doReturn("ace").when(x).foo(); Assert.assertEquals(, "ace"); } @Test(expected = NullPointerException.class) public void test2() { X x = spy(X.class); /** * Using Mockito.when(…).thenReturn(…) * the real method will still be invoked !!! * Read more about Mockito: doReturn() vs when()[…]