Simply put, you could mock foo like this
public String bar() { Foo foo = Mockito.mock(Foo.class); return foo.foo(); }
The problem with this is that foo.foo() essentially not do anything, since you have not determined that #foo() should return when we call the mocked version. Using a more complete example, you can do something like this:
class MyTest { Foo mockedFoo = Mockito.mock(Foo.class); @Before public void setUp() throws Exception { Mockito.when(mockedFoo.foo()).thenReturn("This is mocked!"); } @Test public void testMock() { String returnedFoo = mockedFoo.foo(); Assert.assertEquals("This is mocked!", returnedFoo); } }
source share