56

In my project we already have mockito-core dependency. I want to stub static method for which I need to add mockito-inline dependency. So want to understand the difference between them. Can they co-exists?

malav.parikh
  • 561
  • 1
  • 4
  • 3

3 Answers3

46

As per latest documentation of version 4.2.0 it looks like mockito community has come up with mockito-inline for some experimental features like mocking final classes and methods, mocking static methods etc. So once they get feedback from community they will merge those changes in mockito-core and abolished mockito-inline. Eventually it will be mockito-core only.

https://javadoc.io/doc/org.mockito/mockito-core/latest/org/mockito/Mockito.html#mockito-inline https://javadoc.io/doc/org.mockito/mockito-core/latest/org/mockito/Mockito.html#39

Prakash Boda
  • 808
  • 7
  • 21
  • 1
    Note that the same features are already in mockito-core, you just have to enable them first. – OrangeDog Jun 14 '21 at 09:04
  • 2
    how to enable mocking static method in mockito core? – Ijaz Jun 30 '21 at 18:25
  • 2
    As per this link it looks like you still need to use Mockito inline for static mocking. https://javadoc.io/doc/org.mockito/mockito-core/latest/org/mockito/Mockito.html#static_mocks – Prakash Boda Jul 02 '21 at 05:33
  • 1
    @Ijaz Create a resource called `mockito-extensions/org.mockito.plugins.MockMaker` with content `mock-maker-inline`. See https://github.com/mockito/mockito/wiki/What%27s-new-in-Mockito-2#mock-the-unmockable-opt-in-mocking-of-final-classesmethods. – JojOatXGME Jan 31 '22 at 12:50
7

As of Mockito 5.0.0, mockito-inline is the default mock maker:

This mockmaker [mockito-inline] creates mocks manipulating bytecode equivalent within the original class such that its method implementations hook into the normal Mockito machinery. As a comparison, the subclass mockmaker generates "real" subclasses for mocks, to mimic the same behavior. While the approaches are similar, the inline mockmaker avoids certain restrictions that the JDK imposes. For example, it does not violate module boundaries (introduced in JDK 9, but more heavily used in JDK 17) and avoids the leaking of the creation of the subclass.

mockito-inline was a separate, configuration-free artifact. (If you wanted to use inline mock making with mockito-core, you had to configure the MockMaker extension file.) However, since Mockito 5.3.0, mockito-inline is no longer published!

If you want to (or have to) use subclass mock making after 5.0.0, there is again a separate, configuration free artifact: mockito-subclass.

There are legitimate remaining use cases for the subclass mockmaker. For example, on the Graal VM's native image, the inline mockmaker will not work and the subclass mockmaker is the appropriate choice. Additionally, if you would like to avoid mocking final classes, using the subclass mockmaker is a possibibility. Note however that if you solely want to use the subclass mockmaker to avoid mocking final, you will run into the above mentioned issues on JDK 17+. We want to leave this choice up to our users, which is why we will keep on supporting the subclass mockmaker.

beatngu13
  • 7,201
  • 6
  • 37
  • 66
4

At least with mockito 3.9 mockito-inline "depends on" mockito-core so...you can declare just mockito-inline...they're not mutually exclusive phew.

https://mvnrepository.com/artifact/org.mockito/mockito-inline/3.9.0

rogerdpack
  • 62,887
  • 36
  • 269
  • 388