Radek Skokan
Radek Skokan

Reputation: 1376

Mockito: Mock object based on another

I have a mocked object, say Car porsche. It has 4 wheels, 1 windscreen,1 engine and name Porsche. This is done via when() and thenReturn().

I want to create another mocked Car instance, say trabant, that will behave (again when() and thenReturn()) exactly as the porsche instance, only will return a different name.

Is it possible to create the 2nd mock based on the 1st one without repeating all the when() and thenReturn() steps?

Upvotes: 12

Views: 4568

Answers (1)

rcgeorge23
rcgeorge23

Reputation: 3694

What about creating a private method in your test that sets the common expectations on a given mock object?

Upvotes: 7

Related Questions