简体   繁体   中英

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:

  • what sense has mocking an instance of a class (defining a specific instance name) if when you use Mockito.mock() you get all instances of the same class mocked equally?
  • And the other one is, if I want to define different behaviours of my mocked instances, do I have to declare in the test the correct names of the instances in order to define a different behaviour with when().then() ?

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.

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 eg Powermock, but that is another story).

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".

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM