How to Mock Static method of Util class in Service Implementation | Community
Skip to main content
Adobe Employee
April 14, 2024
Solved

How to Mock Static method of Util class in Service Implementation

  • April 14, 2024
  • 4 replies
  • 6325 views

Hi Team,

 

I have a git repository(Repo A) which has dependent on other main repository (Repo B). In Repo A we have a service implementation. In Repo B we have common code, here we have a util class and it has a static methods. We are calling these static methods from Repo B in Repo A. Now we are writing the Junit implementation for service class in Repo A. I am trying to mock this static method as mentioned below. Mockito.mockStatic(Utils.class); Mockito.when(Utils.getString()).thenReturn("Mocked string"); . i have added dependencies in my Repo A core POM and parent POM files.

 

I am facing issue i am not able to use the static method. I am seeing error "Cannot resolve method 'mockStatic' in 'Mockito'". Can someone help me on this issue. Your help is much appreciated.

 

https://sourcedcode.com/blog/aem/junit/junit5/mocking-static-methods-in-junit5 

 

 

 

Thanks,

Praveena.

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by Mahedi_Sabuj

You can check the below community post
https://experienceleaguecommunities.adobe.com/t5/adobe-experience-manager/junit-for-accesscontrolutil-getusermanager/m-p/665352#M166512 

Additionally, you can explore the GitHub commit where we've mocked the HttpClient.newBuilder static method. https://github.com/MahediSabuj/aem-demo/commit/677d316157b98757490d59fa723ea77602127405 

4 replies

Mahedi_Sabuj
Community Advisor
Mahedi_SabujCommunity AdvisorAccepted solution
Community Advisor
April 14, 2024

You can check the below community post
https://experienceleaguecommunities.adobe.com/t5/adobe-experience-manager/junit-for-accesscontrolutil-getusermanager/m-p/665352#M166512 

Additionally, you can explore the GitHub commit where we've mocked the HttpClient.newBuilder static method. https://github.com/MahediSabuj/aem-demo/commit/677d316157b98757490d59fa723ea77602127405 

Mahedi Sabuj
sravs
Community Advisor
Community Advisor
April 15, 2024

Hi @thanikon , Please make sure you are using latest version of Junit and mockito dependencies.
MockedStatic was introduced in mockito 3.4.3. If your mockito version is previous version of that it won't work. 

Please try with below,

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.9.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.9.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-launcher</artifactId>
<version>1.9.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>5.3.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<version>5.3.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit-addons</groupId>
<artifactId>junit-addons</artifactId>
<version>1.4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.wcm</groupId>
<artifactId>io.wcm.testing.aem-mock.junit5</artifactId>
<version>5.1.0</version>
<scope>test</scope>
</dependency>
EstebanBustamante
Community Advisor and Adobe Champion
Community Advisor and Adobe Champion
April 15, 2024

Hi, 

The problem is that in some Mockito versions, the mockStatic method was disabled by default. There are a couple of ways to configure this, but I think the easiest one is just to use the mockito-inline dependency instead of mockito-core. Try something like this:

<dependency> <groupId>org.mockito</groupId> <artifactId>mockito-inline</artifactId> <version>5.2.0</version> <scope>test</scope> </dependency>

 

References: 

https://wttech.blog/blog/2020/mocking-static-methods-made-possible-in-mockito-3.4.0/

https://github.com/mockito/mockito/issues/2528

 

Hope this helps

Esteban Bustamante
kautuk_sahni
Community Manager
Community Manager
June 19, 2024

@thanikon Did you find the suggestion helpful? Please let us know if you require more information. Otherwise, please mark the answer as correct for posterity. If you've discovered a solution yourself, we would appreciate it if you could share it with the community. Thank you!  

Kautuk Sahni