Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is mockito-inline and how does it work to mock final methods?

Javadocs in Mockito says - "Starting with version 2.7.6, we offer the 'mockito-inline' artifact that enables inline mock making without configuring the MockMaker extension file". What does this mean? How does mockito-inline works ? Why a separate artifact for mockito-inline?

like image 856
freakomonk Avatar asked Nov 13 '18 05:11

freakomonk


People also ask

What does Mockito inline do?

Class Mockito The Mockito library enables mock creation, verification and stubbing. This javadoc content is also available on the https://site.mockito.org/ web page. All documentation is kept in javadocs because it guarantees consistency between what's on the web and what's in the source code.

Can we mock final methods using Mockito?

Configure Mockito for Final Methods and ClassesBefore we can use Mockito for mocking final classes and methods, we have to configure it. Mockito checks the extensions directory for configuration files when it is loaded. This file enables the mocking of final methods and classes.

What does mock maker inline do?

With the inline mock maker, you can mock final classes and methods. You can do this using mock(SomeClazz. class) like you would with every other object you want to mock. In the example, we see it is the same as with non-final classes and methods.

How does Mockito mock work?

Simply put, Mockito works by storing and retrieving method invocation details on mocks using method interception. Method interception is a technique used in Aspect Oriented Programming(AOP) to address cross-cutting concerns in a software system like logging, statistics etc., by intercepting method calls.


1 Answers

I stumbled upon this too and the answer is buried quite deep in the docs, so here it is.

Mockito optionally offers advanced mocking features like mocking final classes. For this to work, they use a different mechanism ("mock maker") that is considered experimental and therefore turned off by default. But:

As a convenience, the Mockito team provides an artifact where this mock maker is preconfigured. Instead of using the mockito-core artifact, include the mockito-inline artifact in your project.

like image 127
rolve Avatar answered Oct 16 '22 08:10

rolve