Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use mockito-inline with Spring-Boot?

Context: We are using Junit 5, Spring-Boot 2.6.3 Spring-Boot comes with its dependency on mockito-core

Problem I am looking to create a mock for a static method. Mockito provides a library (mockito-inline) that allows mocking static methods, however, it works when mockito-core is not directly in dependency. Mockito-inline downloads the compatible mockito-core when required.
(ref: https://frontbackend.com/java/how-to-mock-static-methods-with-mockito)

Possible Solutions

  1. Remove mockito-core from spring-boot - Please help by suggesting how can it be done, without impacting the same dependency being added by Mockito-inline?
  2. There is a problem with my understanding - If this is the case, kindly help me understand it better, with probably an example of using Mockito with Spring-boot to mock static method
like image 766
Mohit Kanwar Avatar asked Jun 09 '26 19:06

Mohit Kanwar


1 Answers

According the documentation on Github.

It can be done via the mockito extension mechanism by creating the file src/test/resources/mockito-extensions/org.mockito.plugins.MockMaker containing a single line:

mock-maker-inline
like image 64
Wythuk Avatar answered Jun 12 '26 08:06

Wythuk