Alessandro Argentieri
Alessandro Argentieri

Reputation: 3215

Mockito mocks refers to all object of the mocked class?

I'm testing my code with Mockito and I have used:

RequestWrapper requestWrapper = mock(RequestWrapper.class);

I thought it mocks the requestWrapper instance of RequestWrapper.class.

Instead, when in the code of the system under test I debug this:

RequestWrapper a = new RequestWrapper(request);
RequestWrapper b = new RequestWrapper(request);
RequestWrapper c = new RequestWrapper(request);

I figure out that a, b, c instances of RequestWrapper are all mocked. So it seems that the instance name of the Mocked class (not mocked Object then as I figured out!) has no importance at all.

So, my questions are:

EDIT: Update

Sorry I got wrong. Only the instance explicitly mocked become a mock. The others are object of the real classes. I got wrong because of all the other mocked dependencies in the real class too.

So it's not true that if I mock an instance of a class, all the other instances of that class are instantiated as mocks. They are object of the real class.

Thank you for your useful answers, I apologize for the error.

Upvotes: 2

Views: 8086

Answers (4)

Alessandro Argentieri
Alessandro Argentieri

Reputation: 3215

Sorry I got wrong. Only the instance explicitly mocked become a mock. The others are object of the real classes. I got wrong because of all the other mocked dependencies in the real class too.

So it's not true that if I mock an instance of a class, all the other instances of that class are instantiated as mocks. They are object of the real class.

Thank you for your useful answers, I apologize for the error.

Upvotes: 0

GhostCat
GhostCat

Reputation: 140553

As the other answers are outlining: you got mocking wrong.

The point is: you do not want to mock "all" instances of a class. To the contrary: you only create mock objects when you have to. A mock is a mean to make it easier (or possible!) to test a specific piece of code. In other words: the mock allows you to control something that is used within that piece of code.

You are therefore only interested in controlling that one specific object that your "code under test" will be using.

You absolutely do not want to control each and any instance of class X when you use mock(X.class) or @Mock X x somewhere in your unit test!

And yes, when you have

X mock1 = mock(X.class);
X mock2 = mock(X.class);

then you need separate when().then() or verify() specifications for both of those objects (in case the default behaviours that mockito provides aren't sufficient). And that is again a consequence of what I wrote above: it should be absolutely clear to you "which" mock "goes where".

Upvotes: 3

Maciej Kowalski
Maciej Kowalski

Reputation: 26542

Each of the separate RequestWrapper objects that are used in the method under test have to be created independently using Mockito.mock() or using the @Mock annotation.

Then each of them would have to be set-up independently using when().then() syntax.

If you are using many mocks of the same class in one test method, then the mock(Class<T> classToMock, String name) would be helpfull in terms of debugging or assertion failure logging.

Upvotes: 1

Adam Siemion
Adam Siemion

Reputation: 16039

Your assumptions on mocks are incorrect.

When your code under test instantiates new objects (new RequestWrapper()) mocks created using Mockito.mock() are not used (unless you are mocking constructors using e.g. Powermock, but that is another story).

Upvotes: 4

Related Questions