SteveSt
SteveSt

Reputation: 1297

Mockito equivalent of EasyMock's MockBuilder

EasyMock has a function called createMockBuilder with which someone can specify a partially mocked class.

Is it possible to do the same with Mockito?

For example in EasyMock some can do the following :

classA mockedA = EasyMock.
      createMockBuilder(A.class).
      withConstructor(B.class,C.class).
      withArgs(b,null).
      addMockedMethod("print").
      createMock();

Is it possible to do the same with Mockito?

Upvotes: 1

Views: 658

Answers (1)

lmat - Reinstate Monica
lmat - Reinstate Monica

Reputation: 7768

Mockito does it a little differently from EasyMock. For instance, whereas in EasyMock, you decide which member functions you want mocked:

EasyMock.createMockBuilder(A.class).addMockedMethod("foo");

in Mockito, all member functions are mocked by default, and you can specify when you want to call an underlying function:

A a = Mockito.mock(A.class);
Mockito.when(a.foo()).thenCallRealMethod();

If you're wanting to mock only a few member functions with Mockito, I can think of two ways to proceed: The example above, and spying.

A a = Mockito.spy(A.class);
Mockito.when(a.foo()).thenReturn("ret");
a.bar(); // Calls the real A.bar() function.

Using a spy, member functions are not mocked by default, but can be mocked selectively. See more information here: http://site.mockito.org/mockito/docs/current/org/mockito/Mockito.html#spy

Upvotes: 1

Related Questions